NSUserDefaults save two arrays leading to crash

Recently i were studying NSUserDefaults,then make a demo as the following:

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
 NSMutableArray *activity_array = [NSMutableArray array];
 NSMutableArray *movie_array = [NSMutableArray array];
 [defaults setObject:activity_array forKey:@"activity"];
 [defaults setObject:movie_array forKey:@"movie"];
 [defaults synchronize];

Then,i try to write the following code,i name it code2:

 NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
 NSMutableArray *array = [userDefault objectForKey:@"activity"];
 [array addObject:@"123"];

the demo still works.
However the demo crash while i replace code2 with the following code:

 NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
 NSMutableArray *array = [userDefault objectForKey:@"movie"];
 [array addObject:@"123"];

As you can see,the difference is the key.
hope the help of you,thanks!


Source: ios

Leave a Reply