0

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.

Wain
  • 118,658
  • 15
  • 128
  • 151
raej2010
  • 117
  • 2
  • 9
  • possible duplicate of [Best way to save to nsuserdefaults for custom class?](http://stackoverflow.com/questions/3000220/best-way-to-save-to-nsuserdefaults-for-custom-class) – user1459524 Mar 05 '14 at 23:30

2 Answers2

2

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.

Wain
  • 118,658
  • 15
  • 128
  • 151
0

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.

Community
  • 1
  • 1
Rok Jarc
  • 18,765
  • 9
  • 69
  • 124