这个问题看起来像是副本,但它不是。我的Contact类符合NSCoding协议,实现方法:
#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是一个字典,当编码到达对它进行编码时,应用程序崩溃。下面是我序列化Contacts数组的方法:
#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"]];
}我应该如何将NSDictionary序列化为属性?谢谢
发布于 2012-09-04 17:35:05
NSPropertyListSerialization类提供了在属性列表对象与XML或优化的二进制格式之间进行相互转换的序列化方法。NSPropertyListSerialization类对象提供了序列化过程的接口;您不需要创建NSPropertyListSerialization的实例。
NSDictionary *propertyList= @{ @"FirstNameKey" : @"Edmund",
@"LastNameKey" : @"Blackadder" };
NSString *errorStr;
NSData *dataRep = [NSPropertyListSerialization dataFromPropertyList:propertyList
format:NSPropertyListXMLFormat_v1_0
errorDescription:&errorStr];
if (!dataRep) {
// Handle error
}https://stackoverflow.com/questions/12260392
复制相似问题