gpt4 book ai didi

ios - 未调用自定义 NSObject iniWithCoder

转载 作者:行者123 更新时间:2023-11-29 02:51:37 24 4
gpt4 key购买 nike

我有一个自定义对象 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;
}

最佳答案

更改if (self == [super init])if (self = [super init])initWithCoder:

试试这个,

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super init]) {
.....
}
return self;
}

关于ios - 未调用自定义 NSObject iniWithCoder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24425050/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com