iOS 笔记六: 协议 Protocols

协议Protocols
1.声明一个协议,看起来像@interface一样,但是少了相应的@implementation.
A. 
@protocol Foo
-(void)someMethod;
-(void)methodWithArgument:(BOOL)argument;
@property (readonly) int readonlyProperty; //getter(only)is part of this protocol
@property NSString * readwriteProperty; //getter and setter are both in the protocol
-(int)methodThatReturnSomething;
@end
以上方法都是必需的,任何实现这个协议的人都需要把它们全部都实现。

B.
@protocol Foo
-(void)someMethod;
@optional
-(void)methedWithArgument:(BOOL)argument;
@property (readonly) int readonlyProperty; //getter(only)is part of this protocol
@property NSString * readwriteProperty; //getter and setter are both in the protocol
-(int)methodThatReturnSomething;
@end
以上协议只有第一个方法是必须要实现的,
只要你实现了someMethod方法,你就可以说你实现Foo协议。

C.
@protocol Foo
-(void)someMethod;
@optional
-(void)methedWithArgument:(BOOL)argument;
@required
@property (readonly) int readonlyProperty; //getter(only)is part of this protocol
@property NSString * readwriteProperty; //getter and setter are both in the protocol
-(int)methodThatReturnSomething;
@end
以上协议中除了methodWithArgument:方法外,其它的都需要实现。

D.
@protocol Foo<Xyzzy>
-(void)someMethod;
@optional
-(void)methedWithArgument:(BOOL)argument;
@required
@property (readonly) int readonlyProperty; //getter(only)is part of this protocol
@property NSString * readwriteProperty; //getter and setter are both in the protocol
-(int)methodThatReturnSomething;
@end
以上协议中除了methodWithArgument:方法外,其它的都需要实现。
除此之外,还要实现Xyzzy中的协议。

E.
@protocol Foo<Xyzzy, NSObject>
-(void)someMethod;
@optional
-(void)methedWithArgument:(BOOL)argument;
@required
@property (readonly) int readonlyProperty; //getter(only)is part of this protocol
@property NSString * readwriteProperty; //getter and setter are both in the protocol
-(int)methodThatReturnSomething;
@end
以上协议中除了methodWithArgument:方法外,其它的都需要实现。
除此之外,还要实现Xyzzy和NSObject协议(这是什么鬼?)。


2.NSObject协议
有如下一些方法:class,isEqual:,isKindOfClass:,description,performSelector:,等等。


3.协议定义在什么地方?
协议定义在头文件中,可以定义在它自己专属的头文件中。
也可以定义在需要它的类的头文件中。
如果协议只被一个类需要,那么把它定义到这个类的头文件中,
其它情况定义到协议自己的头文件中。
例:UIScrollViewDelegate协议定义在UIScrollView.h中。


4.定义了协议之后怎么实现它?
定义了协议之后,类可以在声明@interface的时候声明要实现协议。
如果你不想别人知道你实现了该协议,在私有的@interface中声明要实现协议也是可以的。
例:
#import "Foo.h" //importing the header file that declares the Foo @protocol.
@interface MyClass : NSObject<Foo>  // MyClass is saying it implements the Foo @protocol.
//do not have to declare Foo's methods again here, it's implicit that you implement it
@end

或者你也可以在私有声明中定义:
@interface MyClass() <Foo>
@end

@implementation MyClass
// 不管是哪种声明方式,你都需要在这里实现Foo的@required方法
@end 

接下来需要实现所有的非@optional方法,否则你将面对编译器的愤怒...
如果你不实现@optional方法,你不会得到任何的警告。

到目前为止已经有了协议、实现了协议的类,现在我们需要指向该类对象的指针。
例:
id <Foo> obj = [[MyClass alloc] init]; // 编译器会喜欢这个
id <Foo> obj = [NSArray array];  // 编译器会报错

也可以把协议声明成一个参数:
-(void)giveMefooObject:(id <Foo>)anObjectImplementingFoo;
@property (nonatomic, weak) id <Foo> myFooPorperty; // 也可以作为属性!
如果你调用以上方法但传入一个没有实现Foo协议的对象,那么编译器会报错。
就像静态类型检查,这些都是编译器帮你做的一些事情,运行时其实是一样的。


5.在iOS中使用协议:delegates and dataSources
在iOS中,但一个对象需要一些重要但是又不通用的功能时,经常使用委托(delegate)来实现它。
实现方式是使用一个赋值为确定协议的对象的属性。
@property (nonatomic,weak) id <UISomeObjectDelegate> delegate;
@property (nonatomic,weak) id <UISomeObjectDataSource> dataSource;
请注意这里使用的是weak @property,这是子类提供非通用功能的一种选择。
使用委托来进行跟实现了协议的对象之间的“盲”通信(如MVC)。

dataSource and Views
复杂的UIView通常拥有一个dataSource因为“视图不能拥有数据”。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值