玩转KVC、KVO、NSNotification

一、KVC

KVC,即是指 Key Value Coding,一个非正式的Protocol,提供一种机制来间接访问对象的属性。而不是通过调用Setter、Getter方法访问。

常用方法

-(void)setValue:(id)value forKey:(NSString *)key;//根据key的值来写对象的属性
- (id)valueForKey:(NSString *)key//根据key的值读取对象的属性

1.先定义一个Student类继承NSObject,定义两个属性。

@interface Student : NSObject
@property (copy, nonatomic)NSString *name;
@property (assign, nonatomic)int age;
@end

2.在控制器中创建Student对象。

- (void)testKVC
{
    Student *student = [[Student alloc] init];
    NSLog(@"student init age =%@", [student valueForKey:@"age"]);//未赋值
    [student setValue:[NSNumber numberWithInt:18]forKey:@"age"];//KVC赋值
    NSLog(@"student age = %@", [student valueForKey:@"age"]);
}
结果:
2017-04-07 10:32:56.922 test[2646:59728] student init age =0
2017-04-07 10:32:56.923 test[2646:59728] student age = 18

二、KVO

KVO的是Key Value Observer的缩写,中文是键值观察。这是一个典型的观察者模式,观察者在键值改变时会得到通知,KVO 就是基于 KVC 实现的。

实现KVC只需要3步:
1.谁需要观察,谁注册需要观察的对象的属性

- (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(nullable void *)context;

2.当观察的属性变化时会自动调用下面这个方法,实现它。

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context;

3.取消注册观察

- (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath;

测试代码如下:

@interface TestKVO ()
@property (strong, nonatomic)Student *student;
@end
- (void)testKVO
{
    self.student = [[Student alloc] init];
    [self.student addObserver:self forKeyPath:@"age" options:NSKeyValueObservingOptionNew context:nil];
    [self.student setValue:[NSNumber numberWithInt:18]forKey:@"age"];//此处应放在一个点击事件中

}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
     /*
     * keyPath 属性名(哪个属性被改)
     * object  哪个对象的属性被改了
     * change  属性的修改情况
     * context void * == id 传值
     */
    if ([keyPath isEqualToString:@"age"]) {
        NSLog(@"age is changed! new=%@",[change valueForKey:NSKeyValueChangeNewKey]);
    }
    
}
- (void)dealloc{
    [self.student removeObserver:self forKeyPath:@"age"];    
}

注意:
1.需要强调的是KVO的回调要被调用,属性必须是通过KVC的方法来修改的,如果是调用类的其他方法来修改属性,这个观察者是不会得到通知的。
2.记得要移除观察者,否则程序会崩溃。

三、NSNotification

通知模式:

一个对象能够给其他任意数量的对象广播信息。对象之间可以没有耦合关系。NSNotification(通知),封装了要广播的信息。 NSNotificationCenter(通知中⼼心),管理注册接收消息对象,广播消息。 observer(观察者),需要监测广播信息的对象,即接收信息的对象。 接收信息对象在通知中心进行注册,包括:信息名称、接收信息时的处理方法。对象通过通知中心广播信息,包括:信息名称、信息内容。已经注册过的对象如果不需要接收信息时,在通知中心注销。
实现NSNotification只需要4步:
1.谁需要观察,谁注册通知

- (void)testNSNotification
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(change:) name:@"SendNotification" object:nil];
    //observer 观察者 : self
    //selector 处理消息的方法名: change
    //name 消息通知的名字: SendNotification
    //object 消息发送者 : 表示接收哪个发送者的通知,如果参数为nil,接收所有发送者的通知
}

2.发送消息通知

//一个点击事件中
- (void)action:(UIButton *)btn{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center postNotificationName:@"SendNotification" object:@"sendToNotification" userInfo:nil];
    //notificationName通知名字
    //object发送通知要传的值
    //userInfo有多个值的时候传入字典,没有传nil
}

3.观察者处理消息

- (void)change:(NSNotification *)notification
{
   NSLog(@"%@",notification.object);
}

4.移除观察者

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];//移除所有通知
   //[[NSNotificationCenter defaultCenter] removeObserver:self name:@"SendNotification" object:nil];//移除指定通知    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 在iOS开发过程中,我们经常会听到或者用到KVO,KVC,NSNotificationCenter等,但是很多时候...
    dullgrass阅读 7,295评论 14 133
  • 上半年有段时间做了一个项目,项目中聊天界面用到了音频播放,涉及到进度条,当时做android时候处理的不太好,由于...
    DaZenD阅读 3,136评论 0 26
  • 在编程中,最常见的就是程序的流程取决于你所使用的各种变量和属性的值,根据变量和属性的值确定后面运行的代码,有时会检...
    pro648阅读 1,813评论 2 27
  • 最近发现之前看的东西没一会就忘记了,所以写来好一些,也建议大家吧学到了记录下来这样加深印象,也可以帮助到别人。首先...
    _技术支持阅读 994评论 1 1
  • 快速了解一本书讲了什么,它的基本构成,讲的对不对,和你之间的关系,能不能对你的生活有实质性的帮助和改变,读...
    北冥有鱼为鲲阅读 306评论 0 1

友情链接更多精彩内容