ios - NSCoding:自定义类未归档,但类成员为空/无
<p><p>我想持久化一个 NSDictionary,里面装满了自定义对象到光盘:</p>
<pre><code> NSDictionary *menuList = [initWithDictionary:xmlParser.items];
//here the "Menu List"`s Object are filled correctly
//persisting them to disc:
NSArray*paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *directory = ;
NSString *fileName = ;
NSString *filePath = ;
//saving using NSKeyedArchiver
NSData* archiveData = ;
;
//here the NSDictionary has the correct amount of Objects, but the Objects` class members are partially empty or nil
NSData *data = ;
NSDictionary *theMenu = (NSDictionary*);
</code></pre>
<p>这里是自定义对象的 .m(存储在 NSDictionary 中的对象类型)</p>
<pre><code>- (id)initWithTitle:(NSString*)tTitle level:(NSString*)tLevel state:(NSString*)tState visible:(BOOL)tVisible link:(NSString*)tLink linkType:(NSString*)tLinkType anId:(NSString*)tId {
if ((self = )) {
self.anId = tId;
self.title = tTitle;
self.level = tLevel;
self.state = tState;
self.visible = tVisible;
self.link = tLink;
self.linkType = tLinkType;
}
return self;
}
- (void) encodeWithCoder:(NSCoder *)encoder {
;
;
;
;
;
;
;
}
- (id)initWithCoder:(NSCoder *)decoder {
if(self == ){
self.anId = ;
self.level = ;
self.state = ;
self.visible = ;
self.title = ;
self.link = ;
self.linkType = ;
}
return self;
}
@end
</code></pre>
<p>我不知道为什么对象未正确解档,但对象的成员在某处丢失。我认为 NSCoding 方法中的某个地方一定有错误,但我找不到它,非常感谢任何帮助。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>实现<code>initWithCoder:</code>方法时,需要正确调用super:</p>
<pre><code>if (self = ) {
</code></pre>
<p>正在取消归档的实例具有更多属性,而不仅仅是特定类中的添加。您也不想检查与 <code>self</code> 的相等性,而是希望分配给 <code>self</code>。</p></p>
<p style="font-size: 20px;">关于ios - NSCoding:自定义类未归档,但类成员为空/无,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/18357285/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/18357285/
</a>
</p>
页:
[1]