I am creating a model for MVC and I have an anomaly where, contrary to the Apple Documentation, "The values โโreturned from NSUserDefaults are immutable, even if you set the mutable object to value. , The [[NSUserDefaults standardUserDefaults] objectForKey: @" key "] returns an array mutable .
I created an empty iOS app in Xcode 4D199 to recreate the condition and confirm that it does not respond to other factors in my project.
I install NSMutableArray as shown:
- (void)setupTest { NSMutableArray *mutableArray = [[NSMutableArray alloc] init]; [mutableArray addObject:@"one"]; [mutableArray addObject:@"two"]; [[NSUserDefaults standardUserDefaults] setObject:mutableArray forKey:@"mutableArray_01"]; [[NSUserDefaults standardUserDefaults] synchronize]; }
and, I get an object as shown:
- (void)checkTest { NSMutableArray *mutableArrayInCheck = nil; id whatIsThis = [[NSUserDefaults standardUserDefaults] objectForKey:@"mutableArray_01"]; if([whatIsThis isKindOfClass:[NSMutableArray class]]) { NSLog(@"The array is mutable."); mutableArrayInCheck = (NSMutableArray *)whatIsThis; } if([whatIsThis isKindOfClass:[NSArray class]]) { NSLog(@"The array is immutable."); } if ([whatIsThis isMemberOfClass:[NSMutableArray class]]) { NSLog(@"The array is a member of NSMutableArray class"); } if (mutableArrayInCheck) { [mutableArrayInCheck addObject:@"three"]; NSLog([mutableArrayInCheck description]); } }
Now, according to the documentation for the apple, one would expect the console to display only an immutable line . But when I run the code, the following lines are displayed in the console:
2012-01-05 18:47:33.328 Tester_01[78533:f803] The array is mutable. 2012-01-05 18:47:33.348 Tester_01[78533:f803] The array is immutable. 2012-01-05 18:47:33.349 Tester_01[78533:f803] ( one, two, three )
So, I am wondering if there is something that I am missing here.
For more information, the code that runs the tests is shown:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { BOOL setupKey = NO; if (setupKey) { [self setupTest]; } BOOL checkKey = YES; if (checkKey) { [self checkTest]; } self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
I initially ran the project with setupKey set to YES . After the first run, I changed setupKey to NO . The console log is the result, while setupKey is set to NO . Let me know what you think.