Re: Implied use of Properties
Re: Implied use of Properties
- Subject: Re: Implied use of Properties
- From: Klaus Backert <email@hidden>
- Date: Tue, 6 Apr 2010 23:39:56 +0200
On 6 Apr 2010, at 16:19, Dave wrote:
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.
Yes, and in the OpenGL example a gave, there is indeed something
hardwired: the attributes of the pixel format the OpenGL context is
built upon -- which is no problem for this kind of application.
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!
After getting the OpenGL context property, I always have an assertion
in the debug configuration (see below). And I almost never concatenate
several invocations of methods, including properties. For debugging
purposes I have a lot of assertions in between (may be, I'm a little
bit paranoid, but, well, what would you expect from a mathematician ;-).
@interface MyCustomView : NSView
{
@private
NSOpenGLContext * mContext;
}
...
@property (readonly, retain) NSOpenGLContext *myContext;
...
@end
@implementation MyCustomView
...
@dynamic myContext;
...
- (NSOpenGLContext *) myContext
{
if (mContext == nil)
{
mContext = [[NSOpenGLContext alloc] initWithFormat:
self.myPixelFormat shareContext: nil];
}
return mContext;
}
...
- (void)mySomethingOne
{
NSOpenGLContext *theContext = self. myContext;
MY_ASSERT(theContext != nil);
...
}
- (void)mySomethingTwo
{
NSOpenGLContext *theContext = self. myContext;
MY_ASSERT(theContext != nil);
...
}
@end
There is, of course, a release (and setting to nil) in dealloc, in
order to balance the retain by alloc-init.
Whatever is called first, mySomethingOne or mySomethingTwo, the
context is created at that point during the execution.
-(ClassY*) SetmClassY (ClassY* theClassY)
This should be
- (void)setmClassY: (ClassY *)theClassY
regards
Klaus
_______________________________________________
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