iOS学习程序员

KVC和KVO学习

2017-07-05  本文已影响14人  CoderLWG

KVC解析

+ (BOOL)accessInstanceVariablesDirectly{   
    return YES;
    }

此方法是否返回YES,默认为YES

     - (NSUInteger)countOf<Key>{
   return 2;
   }
   - (id)objectIn<Key>AtIndex:(NSUInteger)index{
   if (index == 0) {
       return @"hanck"; 
      }
   return @"badian";
   }
* 相关变量 **_key** **_isKey** **key** **isKey** 优先级依次降低

KVO使用

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

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    NSLog(@"%@",change);
}

可以手动触发通知,默认是自动触发通知

上一篇 下一篇

猜你喜欢

热点阅读