大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处.
如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;)
最近在Cocos2D编程for Swift中看到以下一个代码片段:
protocol GameMode:class{
var userInterface:CCNode! {get}
func gameplay(mainScene:MainScene,droppedFallingObject:FallingObject)
func gameplay(mainScene:MainScene,caughtFallingObject:FallingObject)
func gameplayStep(mainScene:MainScene,delta:CCTime)->GameOver
}
注意在声明协议名后直接跟了一个class关键字,这是神马意思?
查阅Swift编程手册得知协议中的class关键字用来限制该协议只能应用在类上,原话是这么说的:
“You can limit protocol adoption to class types (and not
structures or enumerations) by adding the class keyword
must always appear first in a protocol’s inheritance
list, before any inherited protocols”
所以如果你无所谓这个限制,你大可不必加class关键字,没有什么区别!