• 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: Object-C question #1
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Object-C question #1


  • Subject: Re: Object-C question #1
  • From: Shawn Erickson <email@hidden>
  • Date: Wed, 8 Dec 2004 08:56:35 -0800


On Dec 7, 2004, at 11:11 PM, M. Uli Kusterer wrote:

-(id)	initWithString: (NSString*)string
{
	if( !(self = [super init]) )
		return nil;

	[self parseString: string];

	return self;
}

Catches the same semantics, and IMHO makes the code more readable if you have a lot of initialization statements.

Actually if I use this form I much preferred to test against nil instead of using the ! operator (actually I always test against nil when dealing with object references/pointer). I find that clearer to understand when scanning code visually, etc. Also I often try to set the variable outside of the if statement clause because doing so can make it easier to debug symbolically.


-(id)	initWithString: (NSString*)string
{
	self = [super init];
	if (self == nil) // or "if (nil == self)" if you are one of those types
		return nil;

	[self parseString: string];

	return self;
}

Or how about this fun way if you want to have one function exist point and avoid deep nesting, etc. (/me puts on flame suit)...

-(id) initWithString:(NSString*)string
{
    do {
        self = [super init];
        if (self == nil) break;

        [self parseString:string];

        if (someBadThingHappened) {
            [self release]; self = nil;
            break;
        }

        [self someOtherValue:value];
    } while(0);

    return self;
}

Or with those "evil" gotos...

-(id) initWithString:(NSString*)string
{
    self = [super init];
    if (self == nil) return nil;


[self parseString:string]; if (someBadThingHappened) goto FAILED;

    [self someOtherValue:value];
    if (someOtherBadThingHappened) goto FAILED;	

    return self;

FAILED:
    [self release]; self = nil;
    return self;
}


-Shawn

_______________________________________________
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


References: 
 >Object-C question #1 (From: Mark Dawson <email@hidden>)
 >Re: Object-C question #1 (From: King Chung Huang <email@hidden>)
 >Re: Object-C question #1 (From: Thomas Davie <email@hidden>)
 >Re: Object-C question #1 (From: "M. Uli Kusterer" <email@hidden>)

  • Prev by Date: Re: Cocoa-dev Digest, Vol 1, Issue 383
  • Next by Date: Re: Creating a Class Instance By Name
  • Previous by thread: Re: Object-C question #1
  • Next by thread: Re: Object-C question #1
  • Index(es):
    • Date
    • Thread