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

811 阅读2分钟

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对应的观察者通知。