How to save NSMutableArrays in NSUserDefaults
i have 4 NSmutablearrays with individual data.
Now i need to save them in NSuserdefaults ind开发者_开发百科ividually and retrieve them individually.
how can i done this,
can any one please post some code.
Thank u in advance.
What are the contents of the arrays? While NSUserDefaults can store arrays, it only works for arrays that contain NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If there's anything else in your array (custom objects, or other Cocoa objects besides the ones mentioned above) you can't just put it straight into NSUserDefaults.
Assuming your objects are right, you can store the array by doing
[[NSUserDefaults standardUserDefaults] setObject:yourArray forKey:@"aKey"];
You can get it back out again with the arrayForKey: method. Note that when you do, it will no longer be a mutable array: you can call mutableCopy on the returned array to get a mutable one. Furthermore, if you mutate the array after storing it in NSUserDefaults
, your changes will not be reflected in the app's preferences.
If you've got an array of objects that won't go into NSUserDefaults (i.e. not on the list above), all is not lost: you need to look into NSCoding and archiving.
精彩评论