今天遇到一个反馈:在程序中用NSKeyedArchiver保存用户资料时,导致程序退出情况。源代码如下:
if (sectionCount >= 5) {
//保存数据到本地,防止每次进入都请求数据
NSString * filePath = nil;
if (canEdit) {
filePath = [Func getAppDocumentsPath:[NSString stringWithFormat:@"myaccount_%d.plist", taonanApi.currentUserId]];
}else {
filePath = [Func getAppTmpPath:[NSString stringWithFormat:@"myaccount_%@.plist", [userProfile objectForKey:@"friendid"]]];
}
[NSKeyedArchiver archiveRootObject:detailProfile toFile:filePath];
}
查找资料后发现原因如下:使用NSKeyedArchiver保存的对象,其自身及以及属性(或集合中的对象)必须是实现了NSCoding协议的对象才能正确保存。而在detailProfile中,有一个UIImage对象被无意中使用NSDictionary的addEntriesFromDictionary添加进来,导致最后保存时程序报错退出。
解决方案:
1、不要向要使用NSKeyedArchiver保存的对象添加没有实现NSCoding协议的对象。
2、如果不确定是否有无意中添加其他不支持的对象,在使用NSKeyedArchiver保存数据前,排除所有没有实现NSCoding的对象。代码如下:
for (NSString * sectkey in detailProfile) {
if ([sectkey isEqualToString:SECTION_0_NAME] || [sectkey isEqualToString:SECTION_1_NAME]) {
NSMutableDictionary * sectData = [[detailProfile objectForKey:sectkey] objectAtIndex:0];
for (NSString * dkey in sectData) {
if (![[sectData objectForKey:dkey] conformsToProtocol:@protocol(NSCoding)]) {
[sectData removeObjectForKey:dkey];
}
}
}
}
最后总结一句:写代码一定要小心,在添加对象到集合、赋值等操作时,能明确的限定范围的,一定要限定范围。否则可能产生和多意料之外的错误。
血的教训啊!!!