• 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
Re: Working with nil values [was Custom bindings in a control/cell pair]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: Working with nil values [was Custom bindings in a control/cell pair]
      • From: Ricky Sharp <email@hidden>
References: 
 >Custom bindings in a control/cell pair (From: Ricky Sharp <email@hidden>)
 >Re: Custom bindings in a control/cell pair (From: Ricky Sharp <email@hidden>)
 >Re: Custom bindings in a control/cell pair (From: Ricky Sharp <email@hidden>)

  • Prev by Date: Re: Accessing System dictionary?
  • Next by Date: Re: Binding name must be same as ivar name?
  • Previous by thread: Re: Custom bindings in a control/cell pair
  • Next by thread: Re: Working with nil values [was Custom bindings in a control/cell pair]
  • Index(es):
    • Date
    • Thread