On May 20, 2010, at 9:47 AM, Thomas Davie wrote:

> 
> On 20 May 2010, at 15:45, Barry Skidmore wrote:
> 
>> If you need to have it set you should create an initWithOptionName: and call 
>> that from your standard init, or return an error stating a missing value is 
>> needed.
>> 
>> Your init should always call down tithe most specific init.
>> 
>> Specific examples of this are available as best practice in several cocoa 
>> and design pattern books.
> 
> I think you've misunderstood what I was asking.
> 
> If I use a setter (in the most specific init), then I don't need to recode 
> (even the most specific init) if I change how the property is implemented.
> 
> If I don't use the setter, I do.
> 
> What I was asking was why you would want to avoid using setters in the init 
> method... Is there some safety issue I've missed?


The problem is that if you later subclass that object and override the setter 
in the subclass, this means that code in the subclass (the setter) is run 
before the subclasses init has run.  This may be a problem if the subclass 
setter assumes that various other values in the subclass are properly 
initialized.  So:

@interface ClassA : NSObject {
        AnObject *ivar;
}
@property (nonatomic, retain) AnObject *ivar;
@end

// A subclass of ClassA that add some sort of logging/transcript
@interface ClassB : ClassA
@property (nonatomic, readonly) id uniqueId;
@end

@implementation ClassA
@synthesize ivar;
- (id) init
{
        self = [super init];
        if (self) {
                self.ivar = some value; // (1)
        }
        return self;
}
@end

@interface ClassB
- (id) init
{
        self = [super init]; // (2)
        if (self) {
                self.uniqueId = [UniqueId uniqueId]; // (4) in keeping with the 
bad style, we call our setter here as well
        }
        return self;
}
- (void) setIvar: (AnObject *) obj
{
        [[Transcript currentTranscript] setProperty: @"ivar" ofObject: 
self.uniqueId toValue: obj ]; // (3)
        [super setIvar: obj];
}
@end

So when you do:

        [[ClassA alloc] init];

everything is fine, but

        [[ClassB alloc] init];

when it gets to (2), calls the super init and at (1) ends up calling code at 
(3).  But self.uniqueId will be nil (since the code at (4) hasn't been reached 
yet), and the -[Transcript setProperty:ofObject:toValue:] may not be able to 
deal with nil as the object parameter.




Glenn Andreas                      gandr...@gandreas.com 
The most merciful thing in the world ... is the inability of the human mind to 
correlate all its contents - HPL

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to