KVC和KVO

作者: 程序狗 | 来源:发表于2023-05-04 17:54 被阅读0次

    KVC(key-value coding)键值编码,就是指iOS的开发中,可以允许开发者通过key名直接访问对象的属性,或者给对象的属性赋值,而不需要调用明确的存取方法。这样可以在运行时动态访问和修改对象的属性.
    KVC的定义都是通过NSKeyValueCoding 这个分类实现的

    下面是这个分类的关键方法

    - (nullable id)valueForKey:(NSString *)key;                          //直接通过Key来取值
    
    - (void)setValue:(nullable id)value forKey:(NSString *)key;          //通过Key来设值
    
    - (nullable id)valueForKeyPath:(NSString *)keyPath;                  //通过KeyPath来取值
    
    - (void)setValue:(nullable id)value forKeyPath:(NSString *)keyPath;  //通过KeyPath来设值
    

    其他的一些方法

    + (BOOL)accessInstanceVariablesDirectly;
    //默认返回YES,表示如果没有找到Set<Key>方法的话,会按照_key,_iskey,key,iskey的顺序搜索成员,设置成NO就不这样搜索
    
    - (BOOL)validateValue:(inout id __nullable * __nonnull)ioValue forKey:(NSString *)inKey error:(out NSError **)outError;
    //KVC提供属性值正确性验证的API,它可以用来检查set的值是否正确、为不正确的值做一个替换值或者拒绝设置新值并返回错误原因。
    
    - (NSMutableArray *)mutableArrayValueForKey:(NSString *)key;
    //这是集合操作的API,里面还有一系列这样的API,如果属性是一个NSMutableArray,那么可以用这个方法来返回。
    
    - (nullable id)valueForUndefinedKey:(NSString *)key;
    //如果Key不存在,且没有KVC无法搜索到任何和Key有关的字段或者属性,则会调用这个方法,默认是抛出异常。
    
    - (void)setValue:(nullable id)value forUndefinedKey:(NSString *)key;
    //和上一个方法一样,但这个方法是设值。
    
    - (void)setNilValueForKey:(NSString *)key;
    //如果你在SetValue方法时面给Value传nil,则会调用这个方法
    
    - (NSDictionary<NSString *, id> *)dictionaryWithValuesForKeys:(NSArray<NSString *> *)keys;
    //输入一组key,返回该组key对应的Value,再转成字典返回,用于将Model转到字典。
    
    KVC设值
    • kvc优先调用set属性值方法。即setter方法
    • 如果没有找到setter方法,会去检查accessInstanceVariablesDirectly(默认值为YES)有没有返回YES,如果返回NO,KVC会执行setValue:forUndefinedKey。如果为YES,会去找_属性值的成员变量
    • 如果没有setter方法,也没有_成员变量,会去搜索_is的成员变量,没有的话会去找is的成员变量
      *如果都没有,会执行setValue:forUndefinedKey。默认是抛出异常
    KVC取值

    同设值,会先去检查getter方法,如果没有找到,会去检查accessInstanceVariablesDirectly,然后按_,_is,is的顺序去搜索成员变量名。

    KVC处理集合

    @avg,@count, @max, @min, @sum

    #import <Foundation/Foundation.h>
    
    @interface Book : NSObject
    @property (nonatomic, copy)  NSString* name;
    @property (nonatomic, assign)  CGFloat price;
    @end
    
    @implementation Book
    @end
    
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            
            Book *book1 = [Book new];
            book1.name = @"The Great Gastby";
            book1.price = 10;
            Book *book2 = [Book new];
            book2.name = @"Time History";
            book2.price = 20;
            Book *book3 = [Book new];
            book3.name = @"Wrong Hole";
            book3.price = 30;
            
            Book *book4 = [Book new];
            book4.name = @"Wrong Hole";
            book4.price = 40;
            
            NSArray* arrBooks = @[book1,book2,book3,book4];
            NSNumber* sum = [arrBooks valueForKeyPath:@"@sum.price"];
            NSLog(@"sum:%f",sum.floatValue);
            NSNumber* avg = [arrBooks valueForKeyPath:@"@avg.price"];
            NSLog(@"avg:%f",avg.floatValue);
            NSNumber* count = [arrBooks valueForKeyPath:@"@count"];
            NSLog(@"count:%f",count.floatValue);
            NSNumber* min = [arrBooks valueForKeyPath:@"@min.price"];
            NSLog(@"min:%f",min.floatValue);
            NSNumber* max = [arrBooks valueForKeyPath:@"@max.price"];
            NSLog(@"max:%f",max.floatValue);
            
        }
        return 0;
    }
    
    打印结果:
    2018-05-05 17:04:50.674243+0800 KVCKVO[35785:6351239] sum:100.000000
    2018-05-05 17:04:50.675007+0800 KVCKVO[35785:6351239] avg:25.000000
    2018-05-05 17:04:50.675081+0800 KVCKVO[35785:6351239] count:4.000000
    2018-05-05 17:04:50.675146+0800 KVCKVO[35785:6351239] min:10.000000
    2018-05-05 17:04:50.675204+0800 KVCKVO[35785:6351239] max:40.000000
    
    

    @distinctUnionOfObjects 去重
    @unionOfObjects 全集
    两者都是返回NSArray

    KVC用法

    • 动态地取值和设值
    • 用KVC来访问和修改私有变量
    • Model和字典转换
    • 修改一些控件的内部属性
    • 操作集合
    • 用KVC实现高阶消息传递

    KVO (key-value observing)使用

    对目标对象的某属性添加观察,当该属性发生变化时,通过触发观察者对象实现的KVO接口方法,来自动的通知观察者。

    - (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context;
    - (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath;
    
    NSKeyValueObservingOptionNew:change字典包括改变后的值
    NSKeyValueObservingOptionOld:change字典包括改变前的值
    NSKeyValueObservingOptionInitial:注册后立刻触发KVO通知
    NSKeyValueObservingOptionPrior:值改变前是否也要通知(这个key决定了是否在改变前改变后通知两次)
    
    处理变更通知

    每当监听的keyPath发生变化了,就会在这个方法中回调

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

    如何实现我们的类不被KVO,需要关闭自动生成KVO通知,然后手动的调用

    @interface Target : NSObject
    {
        int age;
    }
    
    // for manual KVO - age
    - (int) age;
    - (void) setAge:(int)theAge;
    
    @end
    
    @implementation Target
    
    - (id) init
    {
        self = [super init];
        if (nil != self)
        {
            age = 10;
        }
        
        return self;
    }
    
    // for manual KVO - age
    - (int) age
    {
        return age;
    }
    
    - (void) setAge:(int)theAge
    {
        [self willChangeValueForKey:@"age"];
        age = theAge;
        [self didChangeValueForKey:@"age"];
    }
    
    + (BOOL) automaticallyNotifiesObserversForKey:(NSString *)key {
        if ([key isEqualToString:@"age"]) {
            return NO;
        }
    
        return [super automaticallyNotifiesObserversForKey:key];
    }
    
    @end
    

    手动实现setter方法,不进行调用willChangeValueForKey:和didChangeValueForKey方法, automaticallyNotifiesObserversForKey 返回NO,则可以禁用该类的kvo

    KVO和线程

    KVO行为是同步的,并且发生与所观察的值发生变化的同样的线程上。及setter所在的线程

     Son *s = [Son new];
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSLog(@"current thread: %@",[NSThread currentThread]);
            [s addObserver:self forKeyPath:@"age" options:NSKeyValueObservingOptionNew context:nil];
            dispatch_async(dispatch_get_main_queue(), ^{
                s.age = 10;
            });
        });
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
        
        NSLog(@"current thread: %@",[NSThread currentThread]);
    }
    
    输出
    2023-04-20 16:50:57.840429+0800 testUI[43718:25110937] current thread: <NSThread: 0x6000033e7800>{number = 6, name = (null)}
    2023-04-20 16:50:57.855796+0800 testUI[43718:25110670] current thread: <_NSMainThread: 0x6000033a40c0>{number = 1, name = main}
    
    KVO实现

    KVO是通过isa-swizzling实现的,即编译器会被观察对象创造一个派生类,并将所有被观察对象的isa指向这个派生类。如果用户观察了这个目标类的一个属性,那么派生类会重写这个属性的setter方法,并在其中添加进行通知的代码。OC在发消息时,会通过isa指针找打对象所属的类对象(即派生类),所以setter方法其实是调用的派生类的。

    安全气垫实现

    相关文章

      网友评论

          本文标题:KVC和KVO

          本文链接:https://www.haomeiwen.com/subject/tackjdtx.html