Is it necessary to call [super encodeWithCoder] when subclassing a object that implements NSCoding?
Asked Answered
S

1

15

I know that when you write the initWithCoder method of a subclass of an object that implements NSCoding you have to call super initWithCoder (instead of super init), but do I have to call super encodeWithCoder in the implementation of encodeWithCoder?

Synodic answered 6/4, 2012 at 8:30 Comment(0)
C
24

If you inherit from a class that supports Encoding, it is generally adviseable to use [super encodeWithCoder:] in your encodeWithCoder: method, as much as [super initWithCoder:] is to be used in the initWithCoder: method.

Documentation : NSCoding Protocol Reference

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];
    */
}
Census answered 6/4, 2012 at 8:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.