What's the nicest way to do observer/observable in objective-c (iphone version)
2014-04-24 18:04
423 查看
There are two built-in ways of doing observation in Cocoa: Key-Value Observing and notifications. In neither system do you need to maintain or notify a collection of observers yourself; the framework will handle that for you.
Key-Value Observing (KVO) lets you observe a property of an object — including even a property that represents a collection — and be notified of changes to that property. You just need to send the object
the object you want to receive updates, the key path of the property (relative to the receiver) for which you want to receive updates, and the types of updates you want to receive. (There are similar methods you can use if you want to observe a property representing
a collection.)
Notifications are older and heavier-weight. You register with an
usually the default center — an object and selector pair to be passed a notification when an event occurs. The notification object itself can contain arbitrary data via its
and you can choose to observe all notifications of a specific name rather than those that apply to a particular object.
Which should you use in any particular case? In general, if you care about changes to a specific property of a specific object, use Key-Value Observing. That's what it's designed for and it's intentionally lightweight. (Among other uses, it is the foundation
on which Cocoa Bindings are built.) If you care about a change in state that isn't represented by a property, then notifications are more appropriate.
For example, to stay in sync when the user changes the name of a model object, I'd use KVO. To know when an entire object graph was saved, I'd use notifications.
http://stackoverflow.com/questions/165790/whats-the-nicest-way-to-do-observer-observable-in-objective-c-iphone-version
Key-Value Observing (KVO) lets you observe a property of an object — including even a property that represents a collection — and be notified of changes to that property. You just need to send the object
-addObserver:forKeyPath:options:context:passing
the object you want to receive updates, the key path of the property (relative to the receiver) for which you want to receive updates, and the types of updates you want to receive. (There are similar methods you can use if you want to observe a property representing
a collection.)
Notifications are older and heavier-weight. You register with an
NSNotificationCenter—
usually the default center — an object and selector pair to be passed a notification when an event occurs. The notification object itself can contain arbitrary data via its
userInfoproperty,
and you can choose to observe all notifications of a specific name rather than those that apply to a particular object.
Which should you use in any particular case? In general, if you care about changes to a specific property of a specific object, use Key-Value Observing. That's what it's designed for and it's intentionally lightweight. (Among other uses, it is the foundation
on which Cocoa Bindings are built.) If you care about a change in state that isn't represented by a property, then notifications are more appropriate.
For example, to stay in sync when the user changes the name of a model object, I'd use KVO. To know when an entire object graph was saved, I'd use notifications.
http://stackoverflow.com/questions/165790/whats-the-nicest-way-to-do-observer-observable-in-objective-c-iphone-version
相关文章推荐
- How do I find out what version of Linux I'm running?
- 最新iPhone 2.0 beta固件Pwnage教程(mac)
- What happens if I don't retain IBOutlet?
- ios图片命名Icon-120.png - 120x120 iphone & ipod touch(ios7)
- iBet Win Jason Zhang & Power Station Concert Ticket(iBET Lucky Draw, iphone lucky draw casino, lucky
- Learning Perl: 1.2. What Does "Perl" Stand For?
- What's OO?
- What dose "%...D The time taken to serve the request, in microseconds." mean?
- Getting Started With QuickConnect Iphone » Apr 25 2009 8 Great Resources For Learning Iphone OpenGL ES
- What's new in OpenStack Grizzly ?
- What's a namespace?
- Why "I" in Iphone, Imac, Itouch?
- What's stdcall, cdecl, etc....
- iphone&ipad刷机
- 关于__IPHONE_OS_VERSION_MAX_ALLOWED和__IPHONE_OS_VERSION_MIN_ALLOWED的用法
- What’s the Difference Between Sudo & Su?
- iOS iPhone/iPad全屏截…
- iPhone 6 / 6 Plus 设计·适配方案
- Blackberry version "hello world", which use a socket to send msg to a server
- iPhone起步-1: "Hello,World!" in iPhone Development (转)