iOS学习笔记07—Objective-C 委托、非正式协议、正式协议

来源:互联网 发布:palemoon mac os 编辑:程序博客网 时间:2024/06/10 10:32

iOS学习笔记07—Objective-C 委托、非正式协议、正式协议


一、概念:
1、委托(delegate,也叫代理):当一个对象接受到某个事件或者通知的时候,会向它的Delegate对象查询它是否能够响应这个事件或者通知,如果可以,这个对象就会给它的Delegate对象发送一个消息(执行一个方法调用)。在这种机制下,您可以不进行子类化和方法重载,而是将自己的定制代码放到委托对象中,从而避免对复杂对象进行修改。当您感兴趣的事件发生时,复杂对象会将消息发送给您定制的委托对象。您可以通过这种“挂钩”执行自己的定制代码,实现需要的行为。
2、非正式协议(informal protocol):使用类别category来实现,非正式协议是NSObject的一个类别,这样任何类的对象都可以作为委托对象来使用,它可以列出对象能够执行的所有方法,这样用来实现委托, 我们可以使用选择器来判断该非正式协议中是否有这个方法。
3、正式协议(formal protocol):是一个命名的方法列表,与非正式协议相比不同的是,它要求显示的采用协议,采用协议的方法是在类的@interface声明中列出协议的名称,此时,实现协议的类应该遵守协议,承诺实现协议中的所有方法,否则编译器将会发出警告。
正式协议协议类似于C++的纯虚函数,协议只有声明,没有实现,用来在子类中实现,协议中的方法有两类属性,@required(默认)和@optional两种,@required属性的要求实现协议的类必须要实现这种方法,而@optional属性的方法则不要求,如果不确定协议是否被实现,可以使用respondsToSelector:@select()来判断。

二、应用举例

源码如下: 

/*********简单的委托举例************/

@interface A :NSObject

-(void)Log;

@end

@implementation A

-(void)Log{

    NSLog(@"This is A Log");

}

@end


@interface B :NSObject{

   A* delegate;

}

@property (nonatomic,retain)A* delegate;

-(void)callLog;

@end

@implementation B

@synthesize delegate;

-(id)init{

   if (self = [superinit] ) {

        delegate = [[Aalloc]init];

    }

    returnself;

}

-(void)callLog{

    

    NSLog(@"This is B callLog");

    [self.delegateLog];

}

@end

/*********简单的委托举例************/


/*********简单的正式协议举例************/

//协议--begain

@protocol myProtocol

//可选实现的

@optional

-(void)optional_print;

//要求实现的

@required

-(void)required_print;

@end

//协议--end


//使用协议的类--begain

@interface  myClass :NSObject<myProtocol>

-(void)print;

@end

@implementation myClass

-(void)print{

    NSLog(@"This is myClass print");

}

//实现可选实现的协议

-(void)optional_print{

    NSLog(@"This is protocol optional_print");

}

//实现必须实现的协议

-(void)required_print{

    NSLog(@"This is protocol required_print");

}

@end

//使用协议的类--end

/*********简单的正式协议举例************/



/*********简单的非正式协议举例************/

@interface NSObject(myCategory)

-(void)informal_protocol_print;

@end

@implementation NSObject(myCategory)

-(void)informal_protocol_print{

    NSLog(@"This is informal_protocol_print");

}

@end

/*********简单的非正式协议举例************/



/*********正式协议实现委托举例************/

//协议--begain

@protocol myProtocolEx

//可选实现的

@optional

-(void)optional_print;

//要求实现的

@required

-(void)required_print;

@end

//协议--end


@interface myClassEx :NSObject{

   id<myProtocolEx> delegate;

}

@property (nonatomic,assign)id<myProtocolEx> delegate;

-(void)print;

@end

@implementation myClassEx

@synthesize delegate;

-(void)print{

    NSLog(@"This is myClassEx print");

    [self.delegateoptional_print];

    [self.delegaterequired_print];

}

@end


@interface myCall :NSObject<myProtocol>{

   myClassEx *cls;

}

@property (nonatomic,retain)myClassEx *cls;

-(void)callPrint;

@end

@implementation myCall

@synthesize cls;

-(id)init{

   if (self=[superinit]) {

        myClassEx* c = [[myClassExalloc]init];

       self.cls = c;

       cls.delegate = (id)self;

        [crelease];

    }

    returnself;

}

-(void)callPrint{

    NSLog(@"This is myCall callPrint");

    [self.clsprint];

}

-(void)optional_print{

    NSLog(@"This is myCall implemented formal protocol optional_print");

}

-(void)required_print{

    NSLog(@"This is myCall implemented formal protocol required_print");

}


@end

/*********正式协议实现委托举例************/


int main(int argc,char *argv[])

{

    @autoreleasepool {

        

       //委托的使用

       B* b = [[Balloc]init];

        [bcallLog];

        

        [b informal_protocol_print];//非正式协议的使用

        NSLog(@"---------------------------------");

        

       //协议的使用

        myClass *cls = [[myClassalloc]init];

        [clsprint];

        [clsoptional_print];

        [clsrequired_print];

        

        [cls informal_protocol_print];//非正式协议的使用

        NSLog(@"---------------------------------");

        

       //正式协议实现委托的使用

       myCall *call = [[myCallalloc]init];

        [callcallPrint];

        NSLog(@"---------------------------------");

        

       return UIApplicationMain(argc, argv,nil, NSStringFromClass([AppDelegateclass]));

    }

}



总结:非正式协议为一个可以选择性实现的一系列方法列表。非正式协议虽名为协议,但实际上是挂于NSObject上的未实现分类(Unimplemented Category)的一种称谓,Objetive-C语言机制上并没有非正式协议这种东西,OSX 10.6版本之后由于正式协议也可以通过@optional关键字达成相同功用.

    使用正式协议时,是一种更加抽象的方法,基本接口编程;使用非正式协议时,就是对类进行函数或方法扩充,在使用具体类的对象进行调用。不知这样理解对不对?

0 0
原创粉丝点击