Storing NSMutableArray with NSUserDefaults

In my project, I use NSMutableArrayapplications with NSUserDefaults:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

array = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"array"]]; 

and

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

    [[NSUserDefaults standardUserDefaults] setObject:array forKey:@"array"]; 

the problem is that when I write in a class, for example, firstViewController.m, in viewdidload:

NSLog(@"number of element:%d", appDelegate.array.count);

the result is always "2", but if I write

[appDelegate.array removeAllObjects];

then the result of count is "0". When I restart the application, the counter will again be "2". What can I do to have null objects inside the array when the application restarts?

+3
source share
2 answers

- [NSUserDefaults synchronize] "array" applicationWillTerminate. , , .

+4

applicationWillTerminate: , . (, , ) - , UIApplication applicationWillResignActive: delegate.

0

All Articles