Just a small tweak to my suggested pattern for having -init and -initWithCoder 
share implementation. 

Maybe the shared code should be in a method, but if it is, we have to ensure 
that the methods are never overridden and each supercalss/subclass has a unique 
one. To ensure this we could require the method be given a name like 
-(id)_CurrentClassNameInit.

Also I realized the method should return self, so if there is some fatal error, 
it can release itself and cause the real -init or -initWithCoder to return nil.

So, here's my updated pattern suggestion:

@implementation MyClass

- (id) _MyClassInit
{
        self->someCArray = malloc(100);
        self->someValue = 123;
        ...

        if (someFailure)
        {
                [self release];
                return nil;
        }

        return self;
}

- (id) init
{
        self = [super init];
        self = [self _MyClassInit];
        return self;
}

- (id)initWithCoder: (NSCoder*)coder
{
        self = [super initWithCoder: coder];
        self = [self _MyClassInit];
        
        if ([coder containsValueForKey: @"foo"]) [self setFoo: [coder 
decodeObjectForKey: @"foo"]];
        if ([coder containsValueForKey: @"bar"]) [self setBar: [coder 
decodeObjectForKey: @"bar"]];
        ...
        return self;
}

@end

--Eric


_______________________________________________
Gnustep-dev mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/gnustep-dev

Reply via email to