NSMutableDictionaryをNSUserDefaultsに保存する必要があります。私は多くの例をウェブで検索しましたが、十分に関連性のあるものを受け取っていません。誰かがシャットダウン時にNSUserDefaultsにNSMutableDictionaryを保存し、アプリの起動時にそれを取得する小さなコードを投稿できますか?また、コードの配置場所を教えてください。
私は自分でいくつかのコードを持っていますが、それは機能していません。
appDelegate.h-データの取得
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *dat = [defaults objectForKey:@"theKey"];
tmpArray = [[NSMutableArray alloc] init];
tmpArray = [NSKeyedUnarchiver unarchiveObjectWithData:dat];
[self.window makeKeyAndVisible];
[window addSubview:tabBarController.view];
return YES;
}
appDelegate.h-データの保存
- (void)applicationWillTerminate:(UIApplication *)application {
[self saveContext];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *dat = [NSKeyedArchiver archivedDataWithRootObject:tmpArray];
[defaults setObject:dat forKey:@"theKey"];
}
はい、それはアレイ用ですが、NSMutableDictionary用に1つ必要になります。