iOS 笔记六: 协议 Protocols
2015-09-07 20:48
381 查看
协议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.声明一个协议,看起来像@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因为“视图不能拥有数据”。
相关文章推荐
- IOS开发指南学习——数据持久化(Core Data)
- Installation Failure Troubleshooting for iOS
- 问题:In IOS8 wakeups limit
- iOS应用性能调优的25个建议和技巧
- IOS 技术进阶 —— RunLoop 原理和核心机制
- iOS中简单的KVC
- iOS开发系列课程(05) --- 导航视图控制器
- iOS学习篇章2--Category类别
- 小胖说事39-----iOS 获取通讯录中联系人的所有属性
- ios开发,javascript直接调用oc代码而非通过改变url回调方式
- iOS开发 - GCD
- IOS界面跳转的两种方法
- iOS开发中Touch ID的使用
- iOS中tableView上实现搜索功能
- 蓝懿iOS培训日志5 正反向传值及学习心得
- 正反方向传值及打地鼠游戏的实现
- iOS如何把所有页面状态栏的字体颜色都设置为白色
- iOS7自定义视图控制器过渡2-手势交互过渡
- CoreMotion框架-iOS设备的核心运动
- ios-获取当前正在显示的ViewController