• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
init for immutable classes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: init for immutable classes
      • From: Uli Kusterer <email@hidden>
    • Re: init for immutable classes
      • From: Kyle Sluder <email@hidden>
  • Prev by Date: Re: Building a 64-bit Preferences Pane
  • Next by Date: Populating TableView Via Button.
  • Previous by thread: Re: Building a 64-bit Preferences Pane
  • Next by thread: Re: init for immutable classes
  • Index(es):
    • Date
    • Thread