iPhone + NSUserDefault + NSMutableArray
I am trying to save a MutableArray in NSUserDefault as follows:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:ar开发者_JS百科rSelectedPlayers forKey:@"arrSelectedPlayers"];
Than I am trying to get this array:
arrSelectedPlayers = [[prefs mutableArrayValueForKey:@"arrSelectedPlayers"] mutableCopy];
But I am not getting back my array.
Please help.
Regards, Pratik
After setting the mutable array into the user prefers, you need to synchronize
the user defaults object to write the preferences to disk. I'm assuming that your read of the array occurs after the app has restarted.
You need to update your code slightly:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:arrSelectedPlayers forKey:@"arrSelectedPlayers"];
[prefs synchronize];
精彩评论