我有一个自定义对象 ,LevelContent
其中包含一些属性。LevelContent
符合NSCoding
,我已经实现了encodeWithCoder:
和initWithCoder:
方法。我将数据保存并获取到 Parse.com。保存工作正常,如下所示:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self.currentLevelContent];
当我获取数据时,我得到了NSData
正确的,但是当我尝试LevelContent
用下载的数据初始化时,initWithCoder:
永远不会被调用。我尝试LevelContent
用这个加载:
LevelContent *content = [NSKeyedUnarchiver unarchiveObjectWithData:data];
这是编码/解码的代码
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.tiles forKey:@"tiles"];
[aCoder encodeObject:self.attributes forKey:@"attributes"];
[aCoder encodeObject:self.level forKey:@"level"];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self == [super init]) {
self.tiles = [NSMutableArray array];
[self.tiles addObjectsFromArray:[aDecoder decodeObjectForKey:@"tiles"]];
self.attributes = [NSMutableDictionary dictionary];
[self.attributes addEntriesFromDictionary:[aDecoder decodeObjectForKey:@"attributes"]];
self.level = [Level levelWithTopIndex:0 detailIndex:0];
self.level = [aDecoder decodeObjectForKey:@"level"];
}
return self;
}