Quote:
Originally Posted by thh022
My first try using the NSUserDefault where the user can save their own favourites in. I use a NSMutableArray to store the NSUserDefault-objects in, but I can't get it quite right:
Code:
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
NSUserDefaults *temp1 = [NSUserDefaults standardUserDefaults];
[temp1 setObject:@"string 1" forKey:@"name"];
[temp1 synchronize];
[tempArray addObject:temp1];
NSUserDefaults *temp2 = [NSUserDefaults standardUserDefaults];
[temp2 setObject:@"string 2" forKey:@"name"];
[temp2 synchronize];
[tempArray addObject:temp2];
for(int i=0; i<[tempArray count]; i++){
NSUserDefaults *tempNSUserDefaults = [tempArray objectAtIndex:i];
NSLog(@"Test: %@", [tempNSUserDefaults stringForKey:@"name"]);
}
The output is:
Test: string 2
Test: string 2
It seems like the second NSUserDefault overwrite the first one. I'm am missing something?
|
NSUserDefaults is where you store various data and then recover it from there. You store them like this
Code:
[[NSUserDefaults standardUserDefaults] setObject:toDoStates forKey:@"toDoStates"];
(where toDoStates is an NSArray)
and retrieve them like this
Code:
NSArray* toDoStates = [[NSUserDefaults standardUserDefaults] objectForKey:@"toDoStates"];
Have a look for posts about this topic as there are various nuances depending on what you want to save.