绑定和目标/操作?
我目前有一个颜色井,可以跟踪保存在 NSUserDefaults 中的颜色。它绑定到 NSUserDefaultsController。但是,我也想监听颜色的变化,以便我可以相应地更新我的视图。因此,除了绑定之外,我还向我的首选项控制器添加了一个目标/操作,以发布带有颜色的通知。
1)同时拥有目标/操作和绑定有多安全?是否有可能出现滞后或者不同步并报告不同的值?
2)当我在 IBAction 方法中获取颜色时,我应该从用户默认值还是从颜色井中获取它?
这是我的 colorChanged: 操作:
- (IBAction)colorChanged:(id)sender
{
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[colorWell color] forKey:@"color"];
[notificationCenter postNotificationName:@"ColorChangedNotification" object:self userInfo:userInfo];
}
那么我应该这样做:
[NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"color"]];
或:
[colorWell color];
谢谢!
I currently have a color well which keeps track of a color that gets saved in the NSUserDefaults. It is bound to an NSUserDefaultsController. However, I also want to listen for changes to the color so I can update my views accordingly. Therefore, in addition to the binding, I added a target/action to the color well to my preferences controller that posts a notification with the color.
1) How safe is having both target/action and bindings? Is there a possibility that one might lag or they may be out of sync and report different values?
2) When I am getting the color in my IBAction method, should I get it from the user defaults or from the color well?
Here is my colorChanged: action:
- (IBAction)colorChanged:(id)sender
{
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[colorWell color] forKey:@"color"];
[notificationCenter postNotificationName:@"ColorChangedNotification" object:self userInfo:userInfo];
}
So should I be doing this:
[NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"color"]];
or:
[colorWell color];
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想大部分情况下应该没问题。最好的判断方法就是进行测试。
您绝对,绝对应该直接从颜色井中获取它。为什么?保存到用户默认值时可能会出现延迟。哎呀,默认值甚至可以在应用程序终止之前仅保存一次,而且仍然没问题。 (好吧,这并不完全正确,但仍然如此)默认值的主要目的是在应用程序启动之间保留数据,而不是在应用程序的生命周期内保留数据。
I think for the most part, it should be OK. The best way to tell is to test it out.
You should definitely, definitely get it directly from the color well. Why? There could be a lag when saving to the user defaults. Heck, the defaults could even save only once right before the application terminates, and it would still be alright. (OK, this isn't entirely true, but still) The defaults' main purpose is to persist data in between application launches, not during the lifespan of the app.
同时拥有目标/操作和绑定是安全的。如果您使用 NSNotificationCenter 发布通知,则通知将同步传递给观察者。 (有一个明显的警告,它并不神奇——如果观察者 A 在收到通知时向观察者 B 发送消息,则观察者 B 还没有收到通知。多个线程会进一步增加复杂性。)这在 < a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html" rel="nofollow noreferrer">NSNotificationCenter 的文档。
直接从颜色孔中读取颜色速度很快,并且从 IBAction 中读取颜色可能也很好。如果您在应用程序启动时运行代码,最好从用户默认值中读取,因为颜色井的绑定可能尚未更新。
It is safe to have both target/action and bindings. If you post notifications with an NSNotificationCenter, then the notifications are delivered synchronously to the observers. (With the obvious caveat that it is not magic--if observer A sends a message to observer B when it gets the notification, observer B will not have received the notification yet. Multiple threads add further complexity.) This is called out in the documentation for NSNotificationCenter.
Reading the color directly from the color well is fast, and probably fine from an IBAction. If you're running code when the application is starting it is best to read from the user defaults because the color well's bindings might not have been updated yet.