gnustep-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSNumberFormatter segfault when using -initWithCoder


From: Eric Wasylishen
Subject: Re: NSNumberFormatter segfault when using -initWithCoder
Date: Tue, 26 Apr 2011 20:29:35 -0600

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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]