Re: returning nil on initialization
Re: returning nil on initialization
- Subject: Re: returning nil on initialization
- From: Clark Cox <email@hidden>
- Date: Thu, 1 Apr 2004 16:22:34 -0500
On Apr 01, 2004, at 15:21, Daniel Waylonis wrote:
>
>
Speaking of which, is
>
>
- (id)init
>
{
>
if (self = [super init])
>
{
>
...
>
}
>
>
return(self);
>
}
>
>
preferred to:
>
>
- (id)init
>
{
>
if ([super init])
>
{
>
...
>
}
>
>
return(self);
>
}
>
>
I'm under the impression that the result of [super init] and the
>
variable self will always be the same.
Your impression is wrong. There is nothing special about the self
variable (it is actually just a parameter to the function). If you
don't assign the result of [super init], it will not get changed. Run
the following example, and you will see that the value of self is
unchanged by the call to [super init]:
/*-----------------*/
#import <Foundation/foundation.h>
@interface MyBase : NSObject {}
@end
@interface MyDerrived : NSObject {}
@end
@implementation MyBase
-(id)init
{
return nil;
}
@end
@implementation MyDerrived
-(id)init
{
NSLog(@"self, before call to [super init]: %p", self);
[super init];
NSLog(@"self, after call to [super init]: %p", self);
return nil;
}
@end
int main()
{
[[MyDerrived alloc] init];
return 0;
}
/*-----------------*/
--
Clark S. Cox III
email@hidden
http://homepage.mac.com/clarkcox3/
http://homepage.mac.com/clarkcox3/blog/B1196589870/index.html
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
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.