This question will seem a copy, but it is not.
My Contact class confirms to NSCoding protocol, implementing methods:
#pragma mark Encoding/Decoding
-(void)encodeWithCoder:(NSCoder *)aCoder
{
NSLog(@"Encoding");
[aCoder encodeObject:self.firstName forKey:@"firstName"];
NSLog(@"First name encoded");
[aCoder encodeObject:self.lastName forKey:@"lastName"];
NSLog(@"Last name encoded");
[aCoder encodeInt:self.age forKey:@"age"];
NSLog(@"Age encoded");
[aCoder encodeObject:self.phoneNumbers forKey:@"phoneNumbers"];
NSLog(@"Encoding finished");
}
- (id) initWithCoder: (NSCoder *)coder
{
if (self = [super init])
{
[self setFirstName:[coder decodeObjectForKey:@"firstName"]];
[self setLastName:[coder decodeObjectForKey:@"lastName"]];
[self setPhoneNumbers:[coder decodeObjectForKey:@"phoneNumbers"]];
[self setAge:[coder decodeIntForKey:@"age"]];
}
return self;
}
PhoneNumbers is a dictionary, when encoding reaches to encode it, the application crashes. And this is how I serialize Contacts array:
#pragma mark Import/Export
//Export Contacts to file
-(void)exportContactsToFile
{
BOOL done=[NSKeyedArchiver archiveRootObject:self.contacts toFile:[PathUtility getFilePath:@"phonebook"]];
}
//Import Contacts from file
-(void)importContactsFromFile
{
self.contacts = [NSKeyedUnarchiver unarchiveObjectWithFile:[PathUtility getFilePath:@"phonebook"]];
}
how should I serialize NSDictionary as property?
Thanks
The NSPropertyListSerialization class provides the serialization methods that convert property list objects to and from either an XML or an optimized binary format. The NSPropertyListSerialization class object provides the interface to the serialization process; you don’t create instances of NSPropertyListSerialization.