什么是最好的方式做观察者/ observable在objective-c(iphone版) [英] What's the nicest way to do observer/observable in objective-c (iphone version)

查看:107
本文介绍了什么是最好的方式做观察者/ observable在objective-c(iphone版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我习惯于编写Java Swing UI,在那些如果你有一些属性改变,并且你希望你的UI更新,你将实现观察者/ observable模式。在Java中,通常让你的类维护一个监听器列表,通知不同的事件。



我在Mac上使用Objective-C,它有KVC和绑定,似乎工作非常好,并且需要较少的代码。 iPhone SDK似乎没有这个功能,所以我的问题是:
如果我有一个类保存数据更改,我最好的方式注册一个UI组件与该类,使它可以通知需要显示的数据的变化?

解决方案

有两种内置的观察方法可可:关键值观察和通知。在这两个系统中,你需要自己维护或通知观察者的集合;



键值观察(KVO)允许您观察对象的属性,甚至包括表示集合的属性,并且通知该财产的更改。您只需要发送对象 -addObserver:forKeyPath:options:context:传递要接收更新的对象,属性的关键路径(相对于接收者)您想要接收更新以及要接收的更新类型。 (如果您想要观察表示集合的属性,也可以使用类似的方法。)



通知较旧,较重。您使用 NSNotificationCenter (通常是默认中心)注册对象和选择器对,以在事件发生时传递通知。通知对象本身可以通过其 userInfo 属性包含任意数据,您可以选择观察特定名称的所有通知,而不是适用于特定对象的所有通知。 p>

在任何特定情况下应该使用哪一种?一般来说,如果您关心对特定对象的特定属性的更改,请使用键值观察。这是它的设计和它的故意轻量级。 (除了其他用途之外,它是构建Cocoa Bindings的基础)。如果你关心一个属性没有表示的状态变化,那么通知是更合适的。



例如,为了在用户更改模型对象的名称时保持同步,我将使用KVO。要知道整个对象图形何时被保存,我会使用通知。


I'm used to coding Java Swing UIs, and in those if you have some properties that change, and you want your UI to update, you would implement the observer/observable pattern. In Java you do this normally by having your class maintain a list of listeners that it notifies of different events.

I've played with Objective-C on the Mac, and that has KVC and binding which seems to work very nicely, and requires less code. The iPhone SDK doesn't seem to have this functionality though, so my question is: If I have a class that holds data that changes, what's the best way for me to register a UI component with that class so that it can be notified of changes in the data that it needs to display?

解决方案

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 -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 userInfo property, 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.

这篇关于什么是最好的方式做观察者/ observable在objective-c(iphone版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆