我有以下代码来注册观察者:
// This logs some value
NSLog(@"%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"myKey"]);
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleNotifications:)
name:SomeNotification
object:nil];
- (void) handleNotifications: (NSNotification *)notification
{
// This logs null
NSLog(@"%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"myKey"]);
}
为什么myKey
在通知选择器中返回 null?
编辑
I'm trying to save a new value when the value does not exists in the selector, however it always appear to return null in the selector after the app is restarted.
- (void) handleNotifications: (NSNotification *)notification
{
if (![[NSUserDefaults standardUserDefaults] objectForKey:@"myKey"])) {
NSLog(@"Saving");
[[NSUserDefaults standardUserDefaults] setObject: @"ABC", forKey:@"myKey"]
[[NSUserDefaults standardUserDefaults] synchronize];
}
}