I have an NSArray of custom objects that I want to save and restore. Can this be done with NSUserDefaults?
-
Was there any correct answer here? – Joshua Sep 18 '10 at 11:50
-
@Joshua, thanks for providing the example code, it's very helpful. – trillions Mar 26 '12 at 06:55
3 Answers
You can still use NSUserDefaults if you archive your array into NSData
.
For Archiving your array, you can use the following code:
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:myArray] forKey:@"mySavedArray"];
And then for loading the custom objects in the array you can use this code:
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *savedArray = [currentDefaults objectForKey:@"mySavedArray"];
if (savedArray != nil)
{
NSArray *oldArray = [NSKeyedUnarchiver unarchiveObjectWithData:savedArray];
if (oldArray != nil) {
customObjectArray = [[NSMutableArray alloc] initWithArray:oldArray];
} else {
customObjectArray = [[NSMutableArray alloc] init];
}
}
Make sure you check that the data returned from the user defaults is not nil
, because that may crash your app.
The other thing you will need to do is to make your custom object comply to the NSCoder
protocol. You could do this using the -(void)encodeWithCoder:(NSCoder *)coder
and -(id)initWithCoder:(NSCoder *)coder
methods.
EDIT.
Here's an example of what you might put in the -(void)encodeWithCoder:(NSCoder *)coder
and -(id)initWithCoder:(NSCoder *)coder
methods.
- (void)encodeWithCoder:(NSCoder *)coder;
{
[coder encodeObject:aLabel forKey:@"label"];
[coder encodeInteger:aNumberID forKey:@"numberID"];
}
- (id)initWithCoder:(NSCoder *)coder;
{
self = [[CustomObject alloc] init];
if (self != nil)
{
aLabel = [coder decodeObjectForKey:@"label"];
aNumberID = [coder decodeIntegerForKey:@"numberID"];
}
return self;
}

- 10,475
- 1
- 43
- 50

- 15,200
- 21
- 100
- 172
-
Do I just have to add those method definitions or do I have to put code into those methods? – node ninja Sep 06 '10 at 22:14
-
I've edited my post with an example of what you will need to put into those methods. – Joshua Sep 07 '10 at 05:56
NSUserDefaults
cannot write custom objects to file, only ones it knows about (NSArray
, NSDictionary
, NSString
, NSData
, NSNumber
, and NSDate
). Instead, you should take a look at the Archives and Serializations Programming Guide, as well as the NSCoding Protocol Reference, if you're looking to save and restore custom objects to disk. Implementing the protocol is not terribly difficult, and requires very little work.

- 28,308
- 5
- 77
- 83
Custom objects, no. NSUserDefaults only knows about a few basic types (NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary).
Could you use JSON (http://code.google.com/p/json-framework/) to convert your custom object to a string representation, then save an array of those to Defaults? (Using the setObject:forKey: method).
Otherwise, you could look at using sqlite, NSCoder, or even resort to fopen.

- 23,007
- 8
- 61
- 83
-
Even though I'm using custom objects, they are stored in NSArray, which is what I'm trying to save. Shouldn't it work then? – node ninja Sep 06 '10 at 01:47
-
If by "Shouldn't it work?" you mean "Can't I store custom objects in an NSArray?" the answer is yes. If you mean "Can't I store the array of custom objects in NSUserDefaults?" then the answer is no, because NSUserDefaults cannot store arbitrary objects. – Chuck Sep 06 '10 at 02:49