0

I am wondering how it is possible to save NSMutableArray in an NSUserDefaults? I keep getting the following error.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object (
    "<Data: 0x7a1bf830>",
) for key dataList'

Here is my code

   NSUserDefaults *myDefaults = [[NSUserDefaults alloc]
                                initWithSuiteName:@"group.sharingData"];
  [myDefaults setObject:self.dataList forKey:@"dataList"];
  [myDefaults synchronize];
Danger Veger
  • 1,117
  • 1
  • 9
  • 16
  • check this [thread][1] it describes how to add an array to NSUserdefaults [1]: http://stackoverflow.com/questions/19634426/how-to-save-nsmutablearray-in-nsuserdefaults – Abd Al-rhman Taher Badary Mar 24 '15 at 16:31

1 Answers1

2

The problem is that self.dataList contains an object of type Data (which is a custom class of your implementation).

You need to switch to NSData or convert the Data objects to property list objects.

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
  • 1
    Or make your objects `NSCoding` compatible to use `NSKeyedArchiver` for serialization/deserialization needs – JustSid Mar 24 '15 at 16:31
  • @JustSid Making objects conform to `NSCoding` is not enough to put them in `NSUserDefaults`. You also have to do the conversion to `NSData` manually. – Nikolai Ruhe Mar 24 '15 at 16:36
  • Didn't I mention `NSKeyedArchiver` for serialization/deserialization needs? – JustSid Mar 24 '15 at 16:37