KVC、KVO、Notification、Delegate代码实现及比较

1、KVC 便是指 NSKeyValueCoding,一个非正式的Protocol,提供一种机制来间接访问对象的属性。而不是经过调用Setter、Getter方法访问。KVO 就是基于 KVC 实现的关键技术之一。app

 
        Student *student = [[Student alloc] init];
        //经过KVC设置name的值
        [student setValue:@"Jacedy" forKey:@"name"];    //等效于:student.name = @"Jacedy"
        NSString *m_name = [student valueForKey:@"name"];
        NSLog(@"%@", m_name);
        
        Course *course = [[Course alloc] init];
        [course setValue:@"音乐" forKey:@"CourseName"];
        [student setValue:course forKey:@"course"];
        //经过键值径获取CourseName的值(KVC按照键值路径取值时,若是对象不包含指定的键值,会自动进入对象内部,查找对象属性)
        NSString *courseName = [student valueForKeyPath:@"course.CourseName"];
        NSLog(@"课程名称:%@", courseName);
        //经过键值径设置CourseName的值
        [student setValue:@"实验课" forKeyPath:@"course.CourseName"];
        courseName = [student valueForKeyPath:@"course.CourseName"];
        NSLog(@"课程名称:%@", courseName);
        
        //经过KVC设置NSInteger的值(使用KVC间接修改对象属性时,系统会自动判断对象属性的类型,并完成转换)
        [student setValue:@"88" forKeyPath:@"point"];
        NSString *m_point = [student valueForKey:@"point"];
        NSLog(@"分数:%@", m_point);
        
        //经过KVC操做集合
        Student *student1 = [[Student alloc] init];
        Student *student2 = [[Student alloc] init];
        Student *student3 = [[Student alloc] init];
        [student1 setValue:@"65" forKey:@"point"];
        [student2 setValue:@"77" forKey:@"point"];
        [student3 setValue:@"99" forKey:@"point"];
        NSArray *array = [NSArray arrayWithObjects:student1, student2, student3, nil];
        [student setValue:array forKey:@"otherStudent"];
        NSLog(@"其余学生的成绩:%@", [student valueForKeyPath:@"otherStudent.point"]);
        //KVC的简单运算
        NSLog(@"共 %@ 个学生", [student valueForKeyPath:@"otherStudent.@count"]);
        NSLog(@"最高成绩:%@", [student valueForKeyPath:@"otherStudent.@max.point"]);
        NSLog(@"最低成绩:%@", [student valueForKeyPath:@"otherStudent.@min.point"]);
        NSLog(@"平均成绩:%@", [student valueForKeyPath:@"otherStudent.@avg.point"]);
        
        /* KVC须要注意的地方:
         1)key的值必须正确,若是拼写错误,会出现异常;
         2)当key的值没有定义时,valueForUndefinedKey:方法会被调用,若是你本身写了这个方法,key的值出错就会调用到这里来;
         3)由于类key反复嵌套,因此有个keyPath的概念,keyPath就是用点.号来把一个一个key连接起来,这样就能够根据这个路径访问下去;
         4)NSArray、NSSet等都支持KVC
        */

 

2、KVO 的是KeyValue Observe的缩写,中文是键值观察。这是一个典型的观察者模式,观察者在键值改变时会获得通知。iOS中有个Notification的机制,也能够得到通知,但这个机制须要有个Center,相比之下KVO更加简洁而直接。函数

使用步骤:oop

1.注册须要观察的对象的属性addObserver:forKeyPath:options:context:
2.实现observeValueForKeyPath:ofObject:change:context:方法,这个方法当观察的属性变化时会自动调用
3.取消注册观察removeObserver:forKeyPath:context:post

//  JKChild.h

#import <Foundation/Foundation.h>

@interface JKChild : NSObject

@property(nonatomic, assign) NSInteger happyVal;

@end
//  JKChild.m

#import "JKChild.h"

@implementation JKChild

- init {
    if (self = [super init]) {
        self.happyVal = 100;
        //定时器,1秒钟调用一次timerAction:函数
        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    }
    return self;
}

- (void)timerAction:(NSTimer *)timer {
    //方式一:
    self.happyVal--;
    
    //方式二:
//    _happyVal--;      //直接修改不会触发监听,还需经过KVC方式设置
//    [self setValue:[NSNumber numberWithInteger:_happyVal] forKey:@"happyVal"];
}

@end

 

//  JKNurse.h

#import <Foundation/Foundation.h>

@class JKChild;

@interface JKNurse : NSObject

@property(nonatomic, strong) JKChild *child;

- (id)initWithChild:(JKChild *)child;

@end
//  JKNurse.m

#import "JKNurse.h"
#import "JKChild.h"

@implementation JKNurse

- (id)initWithChild:(JKChild *)child {
    if (self = [super init]) {
        
        self.child = [child retain];
        
        //KVO 注册监听,监听JKChild类中happyVal的值变化
        [self.child addObserver:self forKeyPath:@"happyVal" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:@"xxx"];
    }
    return self;
}

// 监听响应
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    NSLog(@"keyPath:%@, object:%@,change:%@, context:%@", object, keyPath, change, context);
}

- (void)dealloc {
    // 移除监听
    [self.child removeObserver:self forKeyPath:@"happyVal"];
    
    [self.child release];
    [super dealloc];
}

@end
//  main.m

#import <Foundation/Foundation.h>
#import "JKChild.h"
#import "JKNurse.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        JKChild *child = [[JKChild alloc] init];
        JKNurse *nurse = [[JKNurse alloc] initWithChild:child];
        
        //加入了定时器,经过runloop使事件持续运行
        [[NSRunLoop currentRunLoop] run];
    }
    return 0;
}

 

3、Notification(通知)ui

//  Child.h

#import <Foundation/Foundation.h>

#define WEEK_INFOMATION @"WEEK"

@interface Child : NSObject

@property (nonatomic, assign) NSInteger sleep;

@end
//  Child.m

#import "Child.h"

@implementation Child

- (instancetype) init
{
    self = [super init];
    if (self != nil) {
        _sleep = 100;
        
        // 添加定时器
        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    }
    return self;
}

- (void)timerAction:(NSTimer *)timer
{
    _sleep -= 4;
    NSLog(@"%ld", (long)_sleep);
    
    if (_sleep < 90) {
        // 获取通知中心的单例后,给指定的名称发送通知
        [[NSNotificationCenter defaultCenter] postNotificationName:WEEK_INFOMATION object:[NSNumber numberWithInteger:_sleep]];
        
        // 中止定时器
        [timer invalidate];
    }
}

@end
//  Father.h

#import <Foundation/Foundation.h>

@interface Father : NSObject

@end
//  Father.m

#import "Father.h"
#import "Child.h"

@implementation Father

- (instancetype)init
{
    self = [super init];
    if (self != nil) {
        // 监听接收通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(weekNotification:) name:WEEK_INFOMATION object:nil];
    }
    return self;
}

- (void)weekNotification:(NSNotification *)notification
{
    NSLog(@"Father received object is : %@", notification.object);
    
    NSLog(@"week up!");
}

@end

Mother类代码与Father类代码类似,此处略过......atom

 

4、Delegate(代理)spa

//  Boss.h

#import <Foundation/Foundation.h>
#import "Sec.h"

@interface Boss : NSObject          //老板类

@property(copy, nonatomic) NSString *name;

@property(weak, nonatomic) id <SecDelegate>delegate;

-(void)work;

@end
//  Boss.m

#import "Boss.h"

@implementation Boss

@synthesize name;

-(void)work
{
    NSLog(@"%@ 正在工做", name);
}

@end
//  Sec.h

#import <Foundation/Foundation.h>
#import "SecDelegate.h"

@interface Sec : NSObject <SecDelegate>         //秘书类

@property(copy, nonatomic) NSString *name;

@end
//  Sec.m

#import "Sec.h"

@implementation Sec

@synthesize name;

-(void)phone
{
    NSLog(@"%@ 接到了电话", name);
}

@end
//  SecDelegate.h

#ifndef SecDelegate_h
#define SecDelegate_h

#import <Foundation/Foundation.h>

@protocol SecDelegate <NSObject>

@optional    // 默认为@required
-(void)phone;   // 接电话

@end

#endif /* SecDelegate_h */
//  main.m
//  代理模式

#import <Foundation/Foundation.h>
#import "Boss.h"
#import "Sec.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        Boss *boss = [[Boss alloc] init];
        [boss setName:@"刘老板"];
        
        Sec *sec = [[Sec alloc] init];
        [sec setName:@"张秘书"];
        
        boss.delegate = sec;
        
        [boss work];
        [sec phone];
    }
    return 0;
}

 

比较代理

1)delegate 的 优点 : 调试

     1.很是严格的语法。全部将听到的事件必须是在delegate协议中有清晰的定义。code

     2.若是delegate中的一个方法没有实现那么就会出现编译警告/错误

     3.协议必须在controller的做用域范围内定义

      4.在一个应用中的控制流程是可跟踪的而且是可识别的;

     5.在一个控制器中能够定义定义多个不一样的协议,每一个协议有不一样的delegates

     6.没有第三方对象要求保持/监视通讯过程。

     7.可以接收调用的协议方法的返回值。这意味着delegate可以提供反馈信息给controller

      缺点 : 

     1.须要定义不少代码:1.协议定义;2.controller的delegate属性;3.在delegate自己中实现delegate方法定义

     2.在释放代理对象时,须要当心的将delegate改成nil。一旦设定失败,那么调用释放对象的方法将会出现内存crash

     3.在一个controller中有多个delegate对象,而且delegate是遵照同一个协议,但仍是很难告诉多个对象同一个事件,不过有可能。

 

2)notification的 优点 :

       1.不须要编写多少代码,实现比较简单;

       2.对于一个发出的通知,多个对象可以作出反应,即1对多的方式实现简单

       3.controller可以传递context对象(dictionary),context对象携带了关于发送通知的自定义的信息

       缺点 : 

       1.在编译期不会检查通知是否可以被观察者正确的处理; 

       2.在释放注册的对象时,须要在通知中心取消注册;

       3.在调试的时候应用的工做以及控制过程难跟踪;

       4.须要第三方对喜好那个来管理controller与观察者对象之间的联系;

       5.controller和观察者须要提早知道通知名称、UserInfodictionary keys。若是这些没有在工做区间定义,那么会出现不一样步的状况;

       6.通知发出后,controller不能从观察者得到任何的反馈信息。

 

3)KVO的 优点 :

        1.可以提供一种简单的方法实现两个对象间的同步。例如:model和view之间同步;

        2.可以对非咱们建立的对象,即内部对象的状态改变做出响应,并且不须要改变内部对象(SKD对象)的实现;

        3.可以提供观察的属性的最新值以及先前值;

        4.用key paths来观察属性,所以也能够观察嵌套对象;

        5.完成了对观察对象的抽象,由于不须要额外的代码来容许观察值可以被观察

       缺点 :

        1.咱们观察的属性必须使用strings来定义。所以在编译器不会出现警告以及检查;

        2.对属性重构将致使咱们的观察代码再也不可用;

        3.复杂的“IF”语句要求对象正在观察多个值。这是由于全部的观察代码经过一个方法来指向;

        4.当释放观察者时不须要移除观察者。

 

4) 效率确定是delegate比NSNotification高。

delegate方法比notification更加直接,最典型的特征是,delegate方法每每须要关注返回值,也就是delegate方法的结果。好比-windowShouldClose:,须要关心返回的是yes仍是no。因此delegate方法每每包含 should这个很传神的词。也就是比如你作个人delegate,我会问你我想关闭窗口你愿意吗?你须要给我一个答案,我根据你的答案来决定如何作下一步。相反的,notification最大的特点就是不关心接受者的态度,我只管把通告放出来,你接受不接受就是你的事情,同时我也不关心结果。因此notification每每用did这个词汇,好比NSWindowDidResizeNotification,那么NSWindow对象放出这个notification后就什么都无论了也不会等待接 受者的反应。

5)KVO和NSNotification的区别:

和delegate同样,KVO和NSNotification的做用也是类与类之间的通讯,与delegate不一样的是:1)这两个都是负责发出通知,剩下的事情就无论了,因此没有返回值;2)delegate只是一对一,而这两个能够一对多。这二者也有各自的特色。

6)delegate针对one-to-one关系,而且reciever能够返回值给sender;notification 能够针对one-to-one/many/none,reciever没法返回值给sender;因此,delegate用于sender但愿接受到reciever的某个功能反馈值,notification用于通知多个object某个事件。

相关文章
相关标签/搜索