KVO,NSNotification,delegate及block区别
来源:互联网 发布:火爆网络的ann是谁 编辑:程序博客网 时间:2024/05/20 20:18
- KVO就是cocoa框架实现的观察者模式,一般同KVC搭配使用,通过KVO可以监测一个值的变化,比如View的高度变化。是一对多的关系,一个值的变化会通知所有的观察者。
NSNotification是通知,也是一对多的使用场景。在某些情况下,KVO和NSNotification是一样的,都是状态变化之后告知对方。NSNotification的特点,就是需要被观察者先主动发出通知,然后观察者注册监听后再来进行响应,比KVO多了发送通知的一步,但是其优点是监听不局限于属性的变化,还可以对多种多样的状态变化进行监听,监听范围广,使用也更灵活。
delegate 是代理,就是我不想做的事情交给别人做。比如狗需要吃饭,就通过delegate通知主人,主人就会给他做饭、盛饭、倒水,这些操作,这些狗都不需要关心,只需要调用delegate(代理人)就可以了,由其他类完成所需要的操作。所以delegate是一对一关系。
block是delegate的另一种形式,是函数式编程的一种形式。使用场景跟delegate一样,相比delegate更灵活,而且代理的实现更直观。
KVO一般的使用场景是数据,需求是数据变化,比如股票价格变化,我们一般使用KVO(观察者模式)。delegate一般的使用场景是行为,需求是需要别人帮我做一件事情,比如买卖股票,我们一般使用delegate。
Notification一般是进行全局通知,比如利好消息一出,通知大家去买入。delegate是强关联,就是委托和代理双方互相知道,你委托别人买股票你就需要知道经纪人,经纪人也不要知道自己的顾客。Notification是弱关联,利好消息发出,你不需要知道是谁发的也可以做出相应的反应,同理发消息的人也不需要知道接收的人也可以正常发出消息。
0 0
- KVO,NSNotification,delegate及block区别
- KVO,NSNotification,delegate及block区别?
- KVO,NSNotification,delegate及block区别
- KVO,NSNotification、Block、Delegate和KVO的区别
- KVC、KVO、NSNotification、delegate 总结及区别
- KVO,NSNotification,delegete及block区别
- iOS 中KVC、KVO、NSNotification、delegate 总结及区别
- iOS 中KVC、KVO、NSNotification、delegate 总结及区别
- iOS 中KVC、KVO、NSNotification、delegate 总结及区别
- iOS 中KVC、KVO、NSNotification、delegate 总结及区别
- iOS 中KVC、KVO、NSNotification、delegate 总结及区别
- iOS 中KVC、KVO、NSNotification、delegate 总结及区别
- iOS 中KVC、KVO、NSNotification、delegate 总结及区别
- iOS 中KVC、KVO、NSNotification、delegate 总结及区别
- iOS 中KVC、KVO、NSNotification、delegate 总结及区别
- iOS 中KVC、KVO、NSNotification、delegate 总结及区别
- iOS 中KVC、KVO、NSNotification、delegate 总结及区别
- iOS 中KVC、KVO、NSNotification、delegate 总结及区别
- 修改 myeclipse10中web-content-root经验
- mysql 去掉重复的有效操作,在对数据库做操作时请切记要做数据备份
- Adobe Photoshop CC Mac简体中文 硬解
- 并发的实质
- LeetCode OJ-20.Valid Parentheses
- KVO,NSNotification,delegate及block区别
- 小白学习Rxjava2,从零开始到实战(一) RxJava2的基础使用
- Android-edittext-XML属性
- How to contribute the code or doc to Nifi?
- 基于注解的Spring MVC控制器
- -bash: ./startup.sh: Permission denied解决
- JavaScript中的查询标识符
- Linux用户管理相关指令
- 国际体验设计大会邀请函,7月12日北京IXDC等你再聚首