OC学习总结(三)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CYXLZZS/article/details/44198695

14、KVC和KVO

》KVC,key-value coding键值编码,通过setValue: forKey: 的模式给对象设置属性的值,有点类似于C#中的反射机制
    1、基本数据类型setValue时必须封装成对象,解封时时自动的
    2、属性为对象时,可以通过键的路径进行设置和访问。setValue: forKeyPath:和valueForKeyPath。path的访问
       是通过点来逐层访问,如Book对象的属性Author,"author.name"为访问作者姓名,
    3、属性的下划线和没有下划线是一样的,如"author.name"和"_author._name"

》KVO,key-value observer键值观察者。主要应用于观察者模式
    1、注册监听
        - (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath
            options:(NSKeyValueObservingOptions)options context:(void *)context;
    2、实现监听
        - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
            change:(NSDictionary *)change context:(void *)context;
    3、移除监听
        - (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath;

15、谓词NSPredicate

》创建谓词 predicateWithFormat,format中可以用>,=,<,>=,<=,&&,and,&&,or,||,in(大括号包含列表或者传入数组),
  beginswith,endswith,contains,like。
  也可以用占位符

》evaluateWithObject判断对象是否满足设定的条件

》过滤数组filteredArrayUsingPredicate和过滤可变数组filterUsingPredicate

16、通知NSNotification

》发出通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"" object: userInfo];

》接受通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector: object];

》处理通知,通过第二步中selector来处理通知

》通知为一对多,缺点是不易于维护和管理,最好用delegate模式

猜你喜欢

转载自blog.csdn.net/CYXLZZS/article/details/44198695
今日推荐