开发者

How to save a NSMutableDictionary in NSUserDefaults

开发者 https://www.devze.com 2023-02-19 16:53 出处:网络
I need to save a NSMutableDictionary in NSUserDefaults. I have searched the web for many examples, but have not received any ones that ar开发者_开发技巧e relevant enough. Can somebody post a small pie

I need to save a NSMutableDictionary in NSUserDefaults. I have searched the web for many examples, but have not received any ones that ar开发者_开发技巧e relevant enough. Can somebody post a small piece of code which will save a NSMutableDictionary in NSUserDefaults at shutdown and retrieve it at launch of the app? Also could you please tell me where to place the code.

I have some code myself, but it is not working.

appDelegate.h - retrieval of data

- (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 - storage of Data

- (void)applicationWillTerminate:(UIApplication *)application {

      [self saveContext];
      NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
      NSData *dat = [NSKeyedArchiver archivedDataWithRootObject:tmpArray];
      [defaults setObject:dat forKey:@"theKey"];
}

Yes, it is for an array, but I will need one for a NSMutableDictionary.


The discussion of applicationWillTerminate:

For applications that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the application. For applications that support background execution, this method is generally not called when the user quits the application because the application simply moves to the background in that case. However, this method may be called in situations where the application is running in the background (not suspended) and the system needs to terminate it for some reason.

To save your object you should put your code into applicationDidEnterBackground: too.

In iOS 4.0 and later, this method is called instead of the applicationWillTerminate: method when the user quits an application that supports background execution. You should use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. You should also disable updates to your application’s user interface and avoid using some types of shared system resources (such as the user’s contacts database). It is also imperative that you avoid using OpenGL ES in the background.


To save a dictionary instead of an array simply replace all occurrences of Array with Dictionary


And fix the leak

tmpArray  = [[NSMutableArray alloc] init]; // not used, not released but `alloc`ed.
tmpArray = [NSKeyedUnarchiver unarchiveObjectWithData:dat];

by removing the first line.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号