iOS使用KVO设置键值观察依赖键

iOS使用KVO设置键值观察依赖键

应用场景:一个对象的属性之间是相互关联的,也就是对象的一个属性依赖于另一个对象的一个获多个属性。如果这些被依赖属性中的任意一个值发生改变,那么原属性的值也会发生相应的改变。

实战:首先定义类Student,Person。Person作为Student的属性定义。 Student中的infomation依赖于Person中的name和age。

@interface Person : NSObject

@property(nonatomic, copy) NSString *name;
@property(nonatomic, assign) NSInteger age;

@end

@interface Student : NSObject

@property(nonatomic, copy) NSString *infomation;
@property(nonatomic, strong) Person *person;

@end

复制代码

要实现上述依赖关系,对象类需要实现以下两点:

  1. 需要手动实现infomation的setter和getter。
  2. 实现keyPathsForValuesAffectingValueForKey: 或 keyPathsForValuesAffectingInfomation方法。这两个方法的作用是告诉系统infomation属性依赖于person中的age和name属性。
@implementation Student

- (instancetype)init {
    if (self = [super init]) {
        self.infomation = @"";
        self.person = [[Person alloc]init];
        self.person.name = @"";
        self.person.age = 0;
    }
    return self;
}

- (NSString *)infomation {
    return [NSString stringWithFormat:@"student_name = %@ | student_age = %ld",self.person.name,self.person.age];
}

- (void)setInfomation:(NSString *)infomation {
    NSArray *array = [infomation componentsSeparatedByString:@"#"];
    if (array && array.count == 2) {
        self.person.name = array[0];
        self.person.age = ((NSNumber *)array[1]).integerValue;
    }
}

+ (NSSet<NSString *> *)keyPathsForValuesAffectingValueForKey:(NSString *)key {
    if ([key isEqualToString:@"infomation"]) {
        NSSet *set = [NSSet setWithObjects:@"person.name", @"person.age",nil];
        return set;
    }
    return nil;
}

//+ (NSSet<NSString *> *)keyPathsForValuesAffectingInfomation {
//    NSSet *set = [NSSet setWithObjects:@"person.name", @"person.age",nil];
//    return set;
//}

@end

复制代码

KVO实现: 1.初始化student,并且为student添加观察者,观察information属性(注意此处是观察information),以及实现observeValueForKeyPath:ofObject:change:context回调方法。 2.点击触发修改person中的name和age。

- (void)viewDidLoad {
    [super viewDidLoad];

    self.student = [[Student alloc]init];
    [self.student setInfomation:@"Charlie#30"];
    
    [self.student addObserver:self forKeyPath:@"infomation" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"infomation"]) {
        NSLog(@"OBSERVE : old infomation = %@ | new infomation = %@", change[NSKeyValueChangeOldKey], change[NSKeyValueChangeNewKey]);
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    self.student.person.name = @"Vivian";
    self.student.person.age = 40;
}

复制代码

输出:

2019-01-11 10:32:42.533291+0800 CLTestKVO[37577:1834387] OBSERVE : old infomation = student_name = Charlie | student_age = 30 | new infomation = student_name = Vivian | student_age = 30

2019-01-11 10:32:42.533465+0800 CLTestKVO[37577:1834387] OBSERVE : old infomation = student_name = Vivian | student_age = 30 | new infomation = student_name = Vivian | student_age = 40

通过输出结果可以看出,当修改了name和age的时候,infomation属性会收到观察者的通知。注意,此处收到了两次通知,分别是修改name和修改age对应的观察者通知。

转载于:https://juejin.im/post/5c38069ee51d45515f245103

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值