谁能告诉我Obj-C类接口中的尖括号"<…>“是做什么的?像这样的…http://snipt.net/robhawkes/cocoa-class-interface
@interface MapMeViewController : UIViewController <CLLocationManagerDelegate,
MKReverseGeocoderDelegate, MKMapViewDelegate, UIAlertViewDelegate> { ... }
在我看来,它们看起来像某种类型声明(考虑到我以前使用PHP和JavaScript的经验),就像我们正在确保MapMeViewController
是一个CLLocationManagerDelegate
、MKReverseGeocoderDelegate
、MKMapViewDelegate
或UIAlertViewDelegate
有关@interface语法的文档似乎没有提到这一点。
发布于 2009-12-16 23:48:19
类接口定义中的尖括号表示您的类符合的协议。
协议几乎类似于Java或C#中的接口,只是Objective-C协议中的方法可以是可选的。
此外,在Objective-C中,您还可以声明变量、参数或实例变量,以符合多个协议。示例
NSObject<NSCoding, UITableViewDelegate> *myVariable;
在这种情况下,类必须是NSObject
或一个子类(只有NSProxy
及其子类会失败),而且它还必须同时符合NSCoding
和UITableViewDelegate
协议。
在Java或C#中,这只能通过实际声明类来实现。
发布于 2009-12-16 19:09:24
尖括号表示protocol。它们类似于其他语言中的接口。
发布于 2009-12-16 21:44:09
您还可以在代码中使用它们,如强制转换,以告知编译器期望一个符合特定协议的对象。
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.noteFetcher sections] objectAtIndex:section];
https://stackoverflow.com/questions/1913935
复制相似问题