Mac/iPhone Category & Protocol

来源:互联网 发布:程序员的贡献 编辑:程序博客网 时间:2024/06/05 15:40

http://blog.codingmylife.com/?p=41

Categories

#import “ClassName.h”
@interface ClassName ( CategoryName )
// method declarations
@end
#import “ClassName+CategoryName.h”
@implementation ClassName ( CategoryName )
// method definitions
@end
 

For example

@interface MyObject : NSObject{
NSNumber *number;
}
- (NSNumber *)number;
@end
@interface MyObject (Setter)
- (void)setNumber:(NSNumber *)newNumber;
@end
@implementation MyObject
- (NSNumber *)number{
return number;
}
- (void)setNumber(NSNumber *)newNumber{
number = newNumber;
}
@end
 

Protocol

@protocol MyProtocol
- (void)requiredMethod;
@optional
- (void)anOptionalMethod;
- (void)anotherOptionalMethod;
@required
- (void)anotherRequiredMethod;
@end
 

For example

1
2
3
4
5
6
7
8
9
10
Inherit multiple protocol. @interface Formatter : NSObject Instance. Protocol *myXMLSupportProtocol = @protocol(MyXMLSupport);
Check if methods are declared
if(![receiver conformsToProtocol:@protocol(MyXMLSupport)]){
//Object does not conform to MyXMLSupport protocol
//If you are expecting receiver to implement methods declared in the
//MyXMLSupport protocol, this is probably an error
}
 
-(NSString*)formattingService:(id)anObject;
———————————————————————

http://icocoa.cn/ocsection/oc-tips/60-category-vs-protocol

Category
Category提供了一种比继承(inheritance)更为简洁的方法来对class进行扩展,我们可以为任何已经存在的class添加方法(不包括数据成员)却不需要访问该class的实现文件。
新添加的方法和原有的方法具有同等的地位,可以访问class的数据成员,并且完全植入到class的继承体系之中,子类同样会继承新添加的方法。 利用category对类进行扩展可以避免使类的继承体系过于臃肿,复杂,降低了维护成本。另外,新添加的方法如果和已经存在的方法具有相同的 prototype,那么新添加的方法将会覆盖已经存在的方法,也就是category使得使得在没有源文件时修改已存在class的 functionality或者清除bug成为可能,所有该class的对象的行为都将发生变化,这一点是继承无法达到的。

Category的缺点:
如果一个已经存在的方法被新方法覆盖,那么将没有任何途径可以对旧方法进行调用。这一点和继承是不同的,子类对父类成员函数重写(override),但我们依然可以对父类中的被重写的成员函数进行调用。
如果有多个category对同一个class的成员函数进行重写,那么具体的行为是未定义。
Category的应用:
对类进行扩展或patch。
使用category或(anonymous category)来模拟实现private method。
使用category(informal protocol)来实现delegation,在cocoa framework中就大量使用了category来声明delegate methods。
利用category把一个庞大的class划分为小块儿来分别进行开发,从而将functionality更好的进行划分,利于维护。

Protocol
根据字面意思,protocol就是一个协议,一个contract。一个class如果采用了某个protocol,那么它就必须要遵守这个协 议。从另外一个角度,只要我遵守了某个protocol或者标准,那我就可以和其它的类进行交互,而且其它类并不需要知道我的内部是如何实现的,例如 一套组合音响,只要其中的dvd,录音机等设备采用的是标准接口,那么它就可以很轻易的被放入这个音响系统中,发挥自己的功能。回到objective c,在cocoa touch framework中利用protocol声明delegate methods是同样的道理,cocoa touch framework,你们想要提供自定义功能或者响应某个事件吗?很简单,只要你们的功能遵守我提供的protocol。(注意,这里之所以采 用protocol来声明delegate methods,而不是cocoa framework中使用informal protocol(category),是因为objective c 2.0 引入了@optional methods,iphone又是在objective c 2.0之后出来的,所以cocoa touch framework中就用protocol替代informal protocol来声明delegate methods。)其实plugin,回调函数都是和protocol相同的思路。

另一方面,objective c只支持单一继承,protocol提供了一个途径来达到多重继承的目的。

另一介绍 Protocol文章:

http://www.cocoachina.com/bbs/read.php?tid-50601.html

协议,是通过网络,计算机使用者进行通讯后,互相进行约定规定的集合。两个类进行通讯,用协议就比较方便。下面是 CocoaChina 版主“angellixf”为新手写的协议入门介绍以及代码例子,希望对刚入门开发者有所帮助
一、说明
1.协议声明了可以被任何类实现的方法
2.协议不是类,它是定义了一个其他对象可以实现的接口
3.如果在某个类中实现了协议中的某个方法,也就是这个类实现了那个协议。
4.协议经常用来实现委托对象。一个委托对象是一种用来协同或者代表其他对象的特殊对象。
5:委托,就是调用自己定义方法,别的类来实现。
6.新特性说明
@optional预编译指令:表示可以选择实现的方法
@required预编译指令:表示必须强制实现的方法

二、定义
.h

@protocol ContactCtrlDelegate
-(void)DismissContactsCtrl;
@end
 
@interface ContactsCtrl : UIViewController {
    id <ContactCtrlDelegate> delegate;
}
@property (nonatomic, assign) id <ContactCtrlDelegate> delegate;
 

.m

  

三、例子
例如:UITextView

@protocol UITextViewDelegate <NSObject>
 
@optional
 
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
- (BOOL)textViewShouldEndEditing:(UITextView *)textView;
 
- (void)textViewDidBeginEditing:(UITextView *)textView;
- (void)textViewDidEndEditing:(UITextView *)textView;
 
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
- (void)textViewDidChange:(UITextView *)textView;
 
- (void)textViewDidChangeSelection:(UITextView *)textView;
 
@end

如果要调用以上这些方法,就必须设置UITextView的委托:TextView.delegate = self;

四、Demo

1、ContactsCtrl.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#import <UIKit/UIKit.h>
 
//定义协议
@protocol ContactCtrlDelegate
-(void)DismissContactsCtrl;
@end
 
@interface ContactsCtrl : UIViewController {
    IBOutlet UINavigationBar *ContactNavBar;
    id <ContactCtrlDelegate> delegate;
}
@property (nonatomic, assign) id <ContactCtrlDelegate> delegate;
 
-(IBAction)canCelBtn:(id)sender;
@end
1
 
2、ContactsCtrl.m
1
@implementation ContactsCtrl
@synthesize delegate;
 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    ContactNavBar.topItem.prompt = @"选取联系人发送短信";
}
 
//调用协议中的方法
-(IBAction)canCelBtn:(id)sender{
    [delegate DismissContactsCtrl];
}

3、ProtocolDemoCtrl.h

1
2
3
4
5
#import <UIKit/UIKit.h>
#import "ContactsCtrl.h"
@interface ProtocolDemoCtrl : UIViewController <ContactCtrlDelegate>{//添加委托
    ContactsCtrl *contactsView;
}

4、ProtocolDemoCtrl.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#import "ProtocolDemoCtrl.h"
#define BARBUTTONADD(SELECTOR) [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:SELECTOR] autorelease];
 
@implementation ProtocolDemoCtrl
@synthesize contactsView;
 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = BARBUTTONADD(@selector(addContactAction:));
}
 
- (void)addContactAction:(id)sender{
    ContactsCtrl *contactView = [[ContactsCtrl alloc] initWithNibName:@"ContactsCtrl" bundle:nil];
    self.contactsView = contactView;
    contactsView.delegate = self;//设置委托
    [self presentModalViewController:contactsView animated:YES];
    [contactView release];
}
 
//实现ContactCtrlDelegate协议中的方法
-(void)DismissContactsCtrl{
    [contactsView dismissModalViewControllerAnimated:YES];
}
原创粉丝点击