![](/img/trans.png)
[英]Calling a method defined in a protocol on a class included with forward declaration
[英]Protocol on method declaration?
我开始使用Nimbus框架,并且第一次遇到这种语法。 看起来他们在方法声明中然后在声明变量时使用某种协议。 我仅在类名之后看到头文件中使用的协议,因此这对我来说是全新的。
- (UIView<NIPagingScrollViewPage>*)pagingScrollView:(NIPagingScrollView *)pagingScrollView pageViewForIndex:(NSInteger)pageIndex {
也:
UIView<NIPagingScrollViewPage>* pageView = nil;
这到底是什么意思? 他们为什么使用这种格式?
该声明确保返回的UIView符合NIPagingScrollViewPage协议。 如果方法尝试返回未声明符合的对象,则编译器将发出警告。
该语法的更常见用法是委托,因为您将允许所有符合协议的类都作为委托,因此可以使用语法来确保该类符合协议。
-(void)setDelegate:(id<SampleDelegate>)del //Makes sure that del conforms to the protocol SampleDelegate, the compiler will emit a warning
这只是声明它正在对该协议进行确认的方式。 否则将显示警告。 然后,您必须使用id。
因此,使用(datatype<protocol>*)variableName
始终是一个好习惯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.