一起学Objective-C - Protocols
来源:互联网 发布:网络论坛和网络社区 编辑:程序博客网 时间:2024/05/21 20:42
Protocols有点像Java中的interface, 但是要更加灵活一点。
Objective-C中有两种Protocol:
非正式的Protocol - 非正式的Protocol在其他语言中基本找不到对应的概念,但是却很有用,之后再详细学习。
正式Protocol - 我们提供一个对象应该响应的方法列表(在编译器可以校验),在运行期也可以检查某个对象是否遵守了某个protocol. 正式Protocol 比较像Java中的interface.
声明正式Protocol
一个正式Protocol声明了一系列的方法, 下例中的@protocol LinkedList <List>表明List是LinkedList的父Protocol , 它将继承所有的List中的方法。
@protocol List- (void) add: (id) item;- (void) remove: (id) item;- getAtIndex: (int)idx;- (void) clear;@end@protocol LinkedList <List>- (void) addFirst: (id)item;- (void) addLast: (id)item;- getFirst;- getLast;@end
如果你需要让一个类遵守一个Protocol, 你要在你的头文件(interface文件)中来声明,同时实现所有需要实现的方法。
@interface BiQueue <LinkedList>{ // instance variables ...} // method declarations ... // [don't need to redeclare those for the LinkedList protocol]- takeFirst- takeLast@end...@implementation BiQueue // must implement both List's and LinkedList's methods ...- add: (id) item{ // ...}- addFirst: (id)item{ // ...}@end
如果需要你可以让一个类遵守多个Protocol, 可以像下例一样声明
@interface ContainerWindow < List, Window > ...@end
使用Protocol
要使用一个正式Protocol, 只需要简单的像对象发Protocol中的消息。 如果你需要类型校验,有下面两种方式。
第一种你使用遵守了Protocol的类名来声明你的变量的类型
BiQueue queue = [[BiQueue alloc] init]; // send a LinkedList message[queue addFirst: anObject];
// alternatively, we may stipulate only that an object conforms to the // protocol in the following way:id<LinkedList> todoList = [system getTodoList];task = [todoList getFirst];...这个例子我们只指明了Protocol,而没有指明具体是那个实现类。
如果你不确信返回的对象是否遵守了某个Protocol,你可以用下面的方法来进行校验
if ([anObject conformsToProtocol: aProtocol] == YES) { // We can go ahead and use the object. }else { NSLog(@"Object of class %@ ignored ... does not conform to protocol", NSStringFromClass([anObject class])); }
最后,你可以像在头文件(interface)中声明的一样,来指明一个对象遵守了多个Protocol。
id <LinkedList, Window> windowContainerOfUnknownClass;
- 一起学Objective-C - Protocols
- 一起学Objective C - 起源
- 一起学Objective-C - String
- objective-c protocols详解
- 一起学Objective-C 苹果的资源
- 一起学Objective-C - 类和对象
- 一起学Objective-C - 内存管理
- 一起学Objective-C - 创建Interface
- 一起学Objective-C - Objective-C中的OO
- 一起学Objective-C - 创建实现类(implementation)
- 一起学Objective-C - 创建实现类(implementation)2
- objective-c protocols and delegates 基础
- Objective-c的Categories和protocols
- Ry’s Objective-C Tutorial---Protocols
- Effective Objective-C 2.0: Delegate and Data Source Protocols
- iOS开发入门教程之Objective-C · 协议(Protocols)
- Objective-C 苹果开发文档 06 Working with Protocols
- 《跟我一起学C++》
- Delphi中JSon SuperObject 使用:数据集与JSON对象互转
- 虚拟机与主机之间文件共享(可复制剪切)的方法:
- dreamweaver随记----在一张表中,有一行要求不与表格的样式一样的 解决办法
- struts配置数据源时,由于tomcat的版本不同而出现的标签属性名不同
- u-boot移植
- 一起学Objective-C - Protocols
- 第8章 SportsStore:导航与购物车 — 精通MVC 3 框架
- 计算机能思考吗?图1专题7:“计算机能否做类比?”
- malloc()问题
- 俞敏洪终于在清华说实话了。。。。。
- linux下验证软件md5值与SHA1
- 第9章 SportsStore:管理 — 精通MVC 3 框架
- rpm详解
- linux内核移植