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

Xai*_*anX 15 cocoa objective-c nscoding

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

Dr.*_*eon 24

如果从一个支持Encoding的类继承,通常建议[super encodeWithCoder:]encodeWithCoder:方法中使用,尽可能[super initWithCoder:]initWithCoder:方法中使用.

文档: NSCoding协议参考

参考: http ://www.cocoadev.com/index.pl?NSCoder

如果类继承自符合的类(NSObject不符合),则应包含[encodeWithCoder:]方法.

//  <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];
    */
}
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢 :) (2认同)