Re: init for immutable classes
Re: init for immutable classes
- Subject: Re: init for immutable classes
- From: Uli Kusterer <email@hidden>
- Date: Fri, 18 Sep 2009 17:35:26 +0200
On 18.09.2009, at 13:10, Jon Hull wrote:
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:
(...) 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...
See here for a rationale on why it could be a bad idea to use
accessors in init methods (and maybe even destructors):
http://zathras.de/blog-defensive-coding-in-objective-c.htm
And since immutable objects are not intended to be mutated at all,
there should be only two places where ivars are changed: init and
dealloc, so I don't think there's any need for mutators (accessors,
for reading, are likely needed anyway).
Cheers,
-- Uli Kusterer
"The witnesses of TeachText are everywhere..."
_______________________________________________
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