gpt4 book ai didi

objective-c - 实现NSCoding的对象子类化时,是否需要调用[super encodeWithCoder]?

转载 作者:太空狗 更新时间:2023-10-30 03:17:53 24 4
gpt4 key购买 nike

我知道当你编写实现 NSCoding 的对象的子类的 initWithCoder 方法时,你必须调用 super initWithCoder(而不是 super init),但是我必须在encodeWithCoder的实现中调用super encodeWithCoder

最佳答案

如果您继承自支持编码的类,通常建议在您的encodeWithCoder: 方法中使用[super encodeWithCoder:],与 [super initWithCoder:] 用于 initWithCoder: 方法。

文档: NSCoding Protocol Reference

引用: http://www.cocoadev.com/index.pl?NSCoder

if the class inherits from a Class that conforms to (NSObject does not conform) then you should include the [encodeWithCoder:] method.

//  <NSCoding> protocol methods

-(void)encodeWithCoder:(NSCoder*)coder
{
[super encodeWithCoder:coder];
/*
[coder encodeObject: theNSStringInstanceVariable];
[coder encodeObject: theNSDictionaryInstanceVariable];
[coder encodeValueOfObjCType:@encode(BOOL) at:&theBooleanInstanceVariable];
[coder encodeValueOfObjCType:@encode(float) at:&theFloatInstanceVariable];
*/
}

关于objective-c - 实现NSCoding的对象子类化时,是否需要调用[super encodeWithCoder]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10041000/

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