Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed to add additional object using NSCoder encodeObject:/decodeObject
    primarykey
    data
    text
    <p>So I have a project that uses the old-style encodeObject:/decodeObject NSCoder serialization. I need to add a new object to the serialization process but previously saved documents will crash as decodeObject tries to decode something that's not there (yet).</p> <pre><code>-(void) encodeWithCoder:(NSCoder *)aCoder { [super encodeWithCoder:aCoder]; [aCoder encodeObject:existingArray]; [aCoder encodeObject:anotherExistingArray]; [aCoder encodeObject:myNewArray]; } -(id) initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if( self != nil ) { NSArray *_existingArray = [aDecoder decodeObject]; NSArray *_anotherExistingArray = [aDecoder decodeObject]; // We crash here, this object doesn't exist in documents saved with only the first two arrays NSArray *_myNewArray = [aDecoder decodeObject]; } return( self ); } </code></pre> <p>I don't think I can convert this to a keyed archive and maintain compatibility with previously saved archives (which is a requirement). Trying to catch the exception that's thrown doesn't seem to help either.</p> <p>Any ideas?</p> <p><strong>UPDATE</strong>: Bah, why is it when you ask a question you almost immediately find the answer yourself?</p> <p>This class was versioned correctly. Previous author had a bug in the version check. So for completeness:</p> <pre><code>+ (void) initialize { if( self == [MyClass class] ) { [self setVersion:2]; // was 1 } } -(void) encodeWithCoder:(NSCoder *)aCoder { [super encodeWithCoder:aCoder]; [aCoder encodeObject:existingArray]; [aCoder encodeObject:anotherExistingArray]; [aCoder encodeObject:myNewArray]; } -(id) initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; int version = [aDecoder versionForClassName:@"MyClass"]; if( self != nil ) { NSArray *_existingArray = [aDecoder decodeObject]; NSArray *_anotherExistingArray = [aDecoder decodeObject]; if( version &gt; 1 ) { NSArray *_myNewArray = [aDecoder decodeObject]; } } return( self ); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload