我需要将OrderedDictionary保存到 NSUserDefaults。
我在这里和许多其他帖子中阅读了如何做到这一点:
-(id)initWithCoder:(NSCoder *)decoder{
if (self != nil){
dictionary = [decoder decodeObjectForKey:@"dictionary"];
array = [decoder decodeObjectForKey:@"array"];
}
return self;
}
-(void)encodeWithCoder:(NSCoder *)encoder{
[encoder encodeObject: dictionary forKey:@"dictionary"];
[encoder encodeObject: array forKey:@"array"];
}
然后我这样称呼它:
OrderedDictionary *od = [OrderedDictionary dictionaryWithDictionary:[businessInfo objectForKey:@"allowedStates"]];
NSData *archivedObject = [NSKeyedArchiver archivedDataWithRootObject:od];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:archivedObject forKey:@"allowedStates"];
[defaults synchronize];
并像这样取消归档:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *archivedObject = [defaults objectForKey:@"allowedStates"];
OrderedDictionary *countryStates = (OrderedDictionary *)[NSKeyedUnarchiver unarchiveObjectWithData:archivedObject];
我看到它archivedObject
不是空的,但是countryStates
是空的,尽管当我保存它时它不是空的。
如何?
我怎样才能成功存档OrderedDictionary
?
已编辑
方法 initWithCoder 被调用,但不是 encodeWithCoder