您的位置:首页 > 移动开发 > IOS开发

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因为“视图不能拥有数据”。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: