Re: Working with nil values [was Custom bindings in a control/cell pair]
Re: Working with nil values [was Custom bindings in a control/cell pair]
- Subject: Re: Working with nil values [was Custom bindings in a control/cell pair]
- From: Ricky Sharp <email@hidden>
- Date: Thu, 24 Feb 2005 12:40:51 -0600
On Thursday, February 24, 2005, at 11:58AM, Ricky Sharp <email@hidden> wrote:
>Time to take a step back a bit and read up on strategies to work with nil values. At first glance, I may just beef up my cell accessors to handle the nil values (basically just substitute them for a default value; e.g. color would be [NSColor blackColor]). My control accessors would then remain simple call-throughs.
While I could use a different pattern in my accessors, I was hoping to instead use value transformers. But after reading this...
<http://developer.apple.com/documentation/Cocoa/Conceptual/ValueTransformers/Concepts/Registration.html>
...the value transformers are not invoked when testing the interface in IB. Ouch! This would have been a perfect solution. I'm definitely going to file an enhancement request as not being able to test in IB is unacceptable (nil values in my case crash). Thus, I cannot currently use value transformers.
Any best practices for handling nil?
The accessor pattern I currently use is like this:
- (NSColor*)myColor
{
[[myColor retain] autorelease];
}
- (void)setMyColor:(NSColor*)aColor
{
[aColor retain];
[myColor release];
myColor = aColor;
}
- (void)dealloc
{
[self setMyColor:nil];
[super dealloc];
}
I'm thinking that I'll keep this (which means nil can be leagally stored), but modify code that uses the getter to respond correctly if the returned value is nil. e.g.
NSColor* theColor = [self myColor];
if (theColor == nil) theColor = [NSColor someDefaultColor];
Or, should I modify my accessors like this:
- (NSColor*)myColor
{
[[myColor retain] autorelease];
}
- (void)setMyColor:(NSColor*)aColor
{
if (aColor != nil)
{
[aColor retain];
[myColor release];
myColor = aColor;
}
else
{
myColor = [[NSColor someDefaultColor] retain];
}
}
- (void)dealloc
{
[myColor release];
myColor = nil;
[super dealloc];
}
I know either will work; just interested in what others do.
--
Rick Sharp
Instant Interactive(tm)
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden