raej2010
raej2010

Reputation: 117

Other ways to save an NSMutableArray in Xcode

I receive an error when i try to save my NSMutableArray to the NSUserDefaults and I have read enough to understand that because my array is like this:

CustomClass *customObject;
NSMutableArray *mutArray = [[NSMutableArray alloc]initWithObjects:customObject,nil];

Because mutArray has a non-property-list object (customObject) it can't be saved neither onto the NSUserDefaults nor in a .plist file, so I need another way to save it.

Upvotes: 0

Views: 124

Answers (2)

Rok Jarc
Rok Jarc

Reputation: 18875

When saving a mutable array (or dictionary) its 'mutability' shouldn't confuse you. You would save it as you would save its non-mutable sibling.

In order to make it mutable again when loading it you should load it and call mutableCopy on the loaded object. I know this is not your question at the moment but i have a feeling it will be soon.

To successfully save an array or dictionary all of the objects (their classes...) that container hold must conform to the <NSCoding> protocol. More on this here on SO.

Upvotes: 0

Wain
Wain

Reputation: 119031

Make CustomClass implement the < NSCoding > protocol, then archive the instance to data. Now you can add the data to the array / plist. Or save it to disk.

Upvotes: 2

Related Questions