Re: Implied use of Properties
Re: Implied use of Properties
- Subject: Re: Implied use of Properties
- From: Dave <email@hidden>
- Date: Tue, 6 Apr 2010 15:19:42 +0100
On 2 Apr 2010, at 22:21, Klaus Backert wrote:
Hi, Dave
There are some typing errors in this code, I think, but anyway,
this might be a case of lazy creation of an object inside a getter
of another object. You will find the same e.g. in Apple's code
examples about OpenGL, where the OpenGL context is an instance
variable of the OpenGL view. Accessing the context is managed by a
getter method or property, respectively, of the view, where, in the
case of the instance variable being nil, the context is created,
stored in the instance variable, and eventually returned. I call
the creation "lazy", because it is done at the latest point during
execution, immediately before it is needed. For me this practice is
normal.
But, may be, I don't understand your question correctly.
Yes, there were a couple of typeo's in my example. I found the code I
in an Apple Developer Sample App and was wondering if it were
considered "best practice" since for me hiding the allocate in an
Accessor method doesn't do anything for readability of the code. Also
as far as I can see, there are a couple of draw-backs to doing it
this way. In the example:
-(ClassY*) mClassY
{
if (mClassY == nil)
{
mClassY = [[ClassY alloc] initWithData:someData]:
}
return mClassY;
}
The reasons I don't like this are:
1. "someData" is hardwired and it's impossible to pass a parameter
to "mClassY" since it's a getter.
2. If either the alloc or initWithData methods fail there is no way
to pass the error back to the caller except by passing nil as the
Class Selector. And if this were done in a statement like:
self.mClassY. mClassYValue = someValue;
I'm not sure what would happen but it wouldn't be good news!
Putting the allocation in an init method would solve the above
problems, In the sample application it was unclear where/if ClassY
was released, I would have thought that at the very least the
following code would have been added:
-(ClassY*) SetmClassY (ClassY* theClassY)
{
if (theClassY == nil)
[mClassy release]
mClassY = theClassY;
}
It's no biggie either way really, it's just I'm working on a new
project and it would be nice to "do the right thing" now, rather than
later on when I have loads more code to worry about.
All the Best
Dave
_______________________________________________
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