[非凡程序员]kvo

#import <Foundation/Foundation.h>

#import "Person.h"

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

    @autoreleasepool {

        // insert code here...

        NSLog(@"Hello, World!");

//        KVO(Key Value Obsevering)

//        键值监听,当属性值发生改变时会回调相应方法

        //1.注册(添加监听)2.设置(属性值)3.通知(回调方法)4.关闭(移除)

        Person *person=[[Person alloc]init];

        [person setValue:@"lily" forKey:@"name"];

        //添加一个监察者

        //[被观察 addObserver:观察者 forKeyPath:观察的对象属性路径 options:新旧值 context:上下文内容,可以设置为空(nil]

        [person addObserver:person forKeyPath:@"name" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:@"名字变了"];

        

        //在观察方写回调方法-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

        //属性变化

        [person setValue:@"mary" forKey:@"name"];

        

        //移除

        [person removeObserver:person forKeyPath:@"name"];

        //练习一:当人的住址发生改变的时候显示地址发生改变,新地址是什么

        [person setValue:@"Sourth Street" forKey:@"address"];

        

        [person addObserver:person forKeyPath:@"address" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];

        [person setValue:@"Street" forKey:@"address"];

        [person removeObserver:person forKeyPath:@"address"];

        //练习二:小红在减肥,当他体重发生变化是让小明通知他。

        Person *xiaoHong=[[Person alloc]init];

        [xiaoHong setValue:@"XiaoHong" forKey:@"name"];

        [xiaoHong setValue:@"90" forKey:@"weight"];

        

        Person *xiaoMing=[[Person alloc]init];

        [xiaoMing setValue:@"XiaoMing" forKey:@"name"];

        [xiaoMing setValue:@"100" forKey:@"weight"];

        

        [xiaoHong addObserver:xiaoMing forKeyPath:@"weight" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];

        [xiaoHong setValue:@"70" forKey:@"weight"];

        

        [xiaoHong setValue:@"120" forKey:@"weight"];

        [xiaoHong removeObserver:xiaoMing forKeyPath:@"weight"];

        

    }

    return 0;

}




#import <Foundation/Foundation.h>


@interface Person : NSObject

@property(nonatomic,strong)NSString *name;

@property(nonatomic,strong)NSString *address;

@property(nonatomic,assign)float weight;

@end


#import "Person.h"


@implementation Person

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{

//    NSLog(@"对象路径是:%@",keyPath);

//    NSLog(@"%@",object);

//    NSLog(@"%@",change);

//    NSLog(@"%@",context);

    if ([keyPath isEqualToString:@"address"]) {

        NSLog(@"住址发生改变,新地址是:%@",change[@"new"]);

    }

    if ([keyPath isEqualToString:@"weight"]) {

        NSLog(@"%@告诉%@,你的体重发生变化了",_name,[object name]);

        if ([change[@"old"] floatValue]<[change[@"new"] floatValue]) {

            NSLog(@"你的体重变重了,加油减肥啊");

        }

        

    }

//    利用KVO设计一个简单的跟踪程序

    //    1.2个类:人和手机

    //    2.两个人,其中一个人监听另一个人的手机定位

    //    3.当手机定位发生改变,监听者得知被监听者的位置信息

    //    4.被监听者在某个路口发现似乎被人跟踪,于是关闭手机定位,并跑开(假定此处移除观察者)

    //    5.被监听者位置改变,监听者无法得知

    

    //练习:在要钱的代码上添加观察,父亲的钱数被父亲观察


你可能感兴趣的:([非凡程序员]kvo)