RE: Returning nil from -init
RE: Returning nil from -init
- Subject: RE: Returning nil from -init
- From: "Mondragon, Ian" <email@hidden>
- Date: Wed, 12 Mar 2003 10:41:39 -0600
oops - too many BOOL's on the brain...
// then you can verify the pdb argument before
// setting it as an ivar & not have to deal with it
- initWithData:(NSData *)pdb
{
if ((self = [super init]))
{
if ([self _parse
Data:pdb])
{
[self set
Data:pdb];
return self;
}
}
return nil;
}
sorry...
- ian
>
-----Original Message-----
>
From: Mondragon, Ian
>
Sent: Wednesday, March 12, 2003 10:05 AM
>
To: email@hidden; email@hidden
>
Subject: RE: Returning nil from -init
>
>
chris,
>
>
i would personally do something more along these lines to avoid this
>
mess:
>
>
--- snip ---
>
>
// create a normal set method to handle the release/retain
>
// of your data ivar
>
- (void)setData:(NSData *)someData
>
{
>
[data autorelease];
>
data = [someData retain];
>
}
>
>
// create a lower level method to verify the NSData
>
// object according to your needs
>
- (BOOL)_parseData:(NSData *)someData
>
{
>
if (someData)
>
{
>
// return YES if things go well
>
}
>
>
return NO;
>
}
>
>
// have your object's -parseData method simply call
>
// _parseData: with it's data ivar
>
- (BOOL)parseData
>
{
>
return [self _parseData:instanceData];
>
}
>
>
// then you can verify the pdb argument before
>
// setting it as an ivar & not have to deal with it
>
- initWithData:(NSData *)pdb
>
{
>
if ((self = [super init]))
>
{
>
if ([self _parseData:pdb])
>
{
>
[self setData:pdb];
>
>
return YES;
>
}
>
}
>
>
return NO;
>
}
>
>
--- snip ---
>
>
- ian
>
>
> -----Original Message-----
>
> From: Chris Ridd [SMTP:email@hidden]
>
> Sent: Wednesday, March 12, 2003 3:19 AM
>
> To: Cocoa Development List
>
> Subject: Returning nil from -init
>
>
>
> I've got an init method that can fail if some of the input contains
>
> incorrect data, and I understand the correct approach is to return nil
>
in
>
> this case. But it isn't clear to me what to do with the partial object
>
> pointed at by self in this case. Is calling dealloc or release
>
> appropriate?
>
>
>
> - (id)initWithData : (NSData *)pdb
>
> {
>
> if (self = [super init]) {
>
> data = pdb;
>
> [data retain];
>
> if ([self parseData] == NO ) {
>
> [self dealloc]; // or release?
>
> self = nil;
>
> }
>
> }
>
> return self;
>
> }
>
>
>
> My -dealloc method does a [data release], but I'm concerned that calling
>
> -dealloc myself is bad style. Can I rely on -release doing the right
>
> thing?
>
>
>
> Cheers,
>
>
>
> Chris
>
> _______________________________________________
>
> cocoa-dev mailing list | email@hidden
>
> Help/Unsubscribe/Archives:
>
> http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
> Do not post admin requests to the list. They will be ignored.
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.