空,我有以下代碼以註冊的觀察者:返回NSUserDefaults的在NSNotificationCenter選擇
// 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?
編輯
我想,當值不存在,在選擇保存新的價值,但它總是出現在應用程序重新啓動後,在選擇器返回null。
- (void) handleNotifications: (NSNotification *)notification
{
if (![[NSUserDefaults standardUserDefaults] objectForKey:@"myKey"])) {
NSLog(@"Saving");
[[NSUserDefaults standardUserDefaults] setObject: @"ABC", forKey:@"myKey"]
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
要解決您的問題,我們需要更多信息 –
我認爲您需要對「返回」實際執行的操作進行一點閱讀。 –
我很震驚,甚至編譯了這段代碼。這種回報不是很接近正確。 –