init for immutable classes
init for immutable classes
- Subject: init for immutable classes
- From: Jon Hull <email@hidden>
- Date: Fri, 18 Sep 2009 04:10:46 -0700
Hello,
I am writing a framework which runs on both Snow Leopard and the
iPhone (i.e. it is entirely foundation stuff).
I always write my init and dealloc methods using the setter functions,
but I am wondering what the proper convention is for immutable
classes? Should I write a private setter method, or is it ok to just
set the variable in init and release it in -dealloc. Thus:
Option 1)
-(id)initWithValue:(id)aValue
{
if(self=[super init]){
[self setValue:aValue];
}
return self;
}
-(void)dealloc
{
[self setValue:nil];
[super dealloc];
}
Option 2)
-(id)initWithValue:(id)aValue
{
if(self=[super init]){
_value = [aValue copy];
}
return self;
}
-(void)dealloc
{
[[self value] release];
[super dealloc];
}
Is ether one better for a particular reason, or are they both ok, and
it is just a matter of style? I am thinking that the second is
probably better, because even a 'private' method could get called from
outside of the class, and I couldn't guarantee it's immutability
anymore. That is so different from what I normally do (I was taught
to always use setter methods) that I wanted to check with the list
first...
Thanks,
Jon
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden