The MiscKit

Object Versioning


Home
Hot News
About
Press Kit
FAQ
Documentation
Download
Resources
Services
Search
Credits
Links
Contact Us


It is important that your objects support versioning--especially those which are palettized. By doing this, changes in object's instance variables won't render .nib files with the old object unloadable in InterfaceBuiler. We strongly recommend that you use versioning in all your objects, however. If your object contains -read: and -write: methods, you should definitely use versioning!

Here, contributed by Jason Beaver, is some source which clearly shows what you need to do to make sure that versioning of an object is handled correctly, with the important code in boldface:

MyClass.h

// Copyright (c) 1995 Jason Beaver.
// Use is governed by the MiscKit license.

#import <appkit/appkit.h >

@interface MyClass : MySuperClass
{
    id      otherObjects;
    id      delegate;
    BOOL    allowsChanges;
}

+ initialize;

...

- read:(NXTypedStream *)stream;
- write:(NXTypedStream *)stream;

@end

MyClass.m

// Copyright (c) 1995 Jason Beaver.
// Use is governed by the MiscKit license.

#import <misckit/MyClass.h>

#define CURRENT_VERSION 3

@implementation MyClass

+ initialize
{
    if ( self == [MyClass class] )
        [MyClass setVersion:CURRENT_VERSION];

    return self;
}

...

- read:(NXTypedStream *)stream
{
    int version;

    [super read:stream];

    version = NXTypedStreamClassVersion(stream,"MyClass");

    if ( version == CURRENT_VERSION ) {
        NXReadTypes(stream,"@@I",&otherObjects,&delegate,&allowsChanges);
    } else if ( version == 2 ) {
        NXReadTypes(stream,"@@",&otherObjects,&delegate);
        allowsChanges = YES;
    } else if ( version == 1) {
        NXReadType(stream,"@",&otherObjects);
        delegate = nil;
        allowsChanges = YES;
    }
    return self;
}

- write:(NXTypedStream *)stream
{
    [super write:stream];
    NXWriteTypes(stream,"@@I",&otherObjects,&delegate,&allowsChanges);
    return self;
}

@end

Note that the instance variables archived are an example set; your object will obviously use different variables. This simply demonstrates the general idea. The above code is not the only way to do object versioning, so feel free to do it your own way if you have a better (or preferred) method.

The basic idea is:

  • Write out the latest version (this is not a hard and fast rule...you might want to write out an object which can be read back in by an older version of your program),
  • Read the version of the object from the NXTypedStream.
  • Read the data which was archived in that version of the NXTypedStream.
  • Set any instance variables which have been added to the class since that version to reasonable values which make the object function as it did in that version.



Questions? Contact our webmaster via email to don@misckit.com. .