0

I have an array containing either:

  • Nothing
  • Only one string object
  • Several custom objects
  • A string AND several custom objects

to NSUserDefaults.

Short background about this:

I have a core data relationsship between two objects. So any object of Entity A can belong to a category contained in Entity B or belong to no category. I have a search window where you can look of course for specific categories and/or objects that belong to no category. This no category I basically save in my filter array as a specific string.

But I always get the error:

Attempt to insert non-property list object.

This is as far as I have researched now because of my custom objects. But can I also store in this array the string and mix the object types? I also thought about assigning to every object a default category but I think it is cleaner with no category and just work around with this string at the only place where I need it, when using the filter within my app.

Bista
  • 7,869
  • 3
  • 27
  • 55
MichiZH
  • 5,587
  • 12
  • 41
  • 81
  • Check this: Attempt to set a non-property-list object as an NSUserDefaults](http://stackoverflow.com/questions/19720611/attempt-to-set-a-non-property-list-object-as-an-nsuserdefaults). It is due to adding custom object into NSUserDefaults. It is not permitted. – Bista Jan 08 '16 at 07:29

1 Answers1

0

You need to use NSEncode and Decode for this purpose, try following steps to achieve this

1 In .m file of your model

Add encode decode on your model class like this

- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.id forKey:@"ID"];
[aCoder encodeObject:self.address_1 forKey:@"address_1"];
//All of your properties here
}

-(id)initWithCoder:(NSCoder *)aDecoder{
if(self = [super init])
{
    self.id = [aDecoder decodeObjectForKey:@"id"];
    self.address_1 = [aDecoder decodeObjectForKey:@"address_1"];
   //All of your properties here

}
return self;
 }

2 When saving it to NSDefaults use archiving first like this

 [[NSUserDefaults standardUserDefaults]setObject:[NSKeyedArchiver archivedDataWithRootObject:info] forKey:kUserInfo];

3 When getting object use this

[NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults]objectForKey:kUserInfo]]
Mayank Patel
  • 3,868
  • 10
  • 36
  • 59
Arslan Asim
  • 1,232
  • 1
  • 11
  • 29