1

Up until now, I've been using NSUserDefaults to save my NSMutableDictionaries, but now I want to save a dictionary which would look like this: It'll hold different Car objects with keys: the model of the car. The Car class will have a dictionary of its basic characteristics and a dictionary of Person objects (who use the car). Every Person class will have personal information as properties. I can do everything else, but not the saving of the first NSMutableDictionary which will hold all of that info.(dictionaries must contain only non-property values error) What is an appropriate way to save it?

martin
  • 1,007
  • 2
  • 16
  • 32

2 Answers2

2

You have to use an archiver like NSKeyedArchiver to serialise / deserialize your objects to an NSData archive.

All objects in your graph

  • Car
  • Person
  • All properties of these objects not being a value-object (i.e not NSString, NSDate, NSArray, NSNumber,...)

must adopt the NSCoding protocol. To do so you use:

-(id)initWithCoder:(NSCoder *)coder
{
  // Tell the unarchiver how to read your object properties from the archive
  _oneObjectProperty = [coder decodeObjectForKey:@"propertyKey"];
  .....
}

-(void)encodeWithCoder:(NSCoder *)coder
{
  // Tell the archiver how to serialise your object properties
  [coder encodeObject:_oneObjectProperty ForKey:@"propertyKey"];
  ....
}

You'll find code example in here: Why NSUserDefaults failed to save NSMutableDictionary in iPhone SDK?

Community
  • 1
  • 1
Eric Genet
  • 1,260
  • 1
  • 9
  • 19
0

You could use NSKeyedArchiver to save objects and NSKeyUnarchiver to retrieve them.

soryngod
  • 1,827
  • 1
  • 14
  • 13