Re: semantics of init
Re: semantics of init
- Subject: Re: semantics of init
- From: Kyle Moffett <email@hidden>
- Date: Tue, 3 Jun 2003 23:13:30 -0400
The most common way I have seen of doing this is:
@implementation MyClass
- (MyClass *) init {
if (self = [super init]) { /* Assign ourselves to whatever our
superclass returns */
_myInt = 0; /* Init instance variables */
_myString = @"Hello World!!!"; /* Here is a static NSString */
_myId = [[NSMutableArray alloc] init]; /* A mutable NSArray */
if (random()%2) { /* We fail 1/2 the time, randomly */
[self dealloc]; /* This releases anything we have allocated
already */
return (self = nil); /* Return nil to any subclasses */
}
}
return self; /* Return whatever we are given */
}
- (void) dealloc {
if (_myString) [_myString release];
if (_myId) [_myId release];
}
@end
Also, have a look at the huge retain/release threads in the archives.
Very good discussions on memory management.
HTH,
Kyle Moffett
-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCM/CS/IT/U d- s++:- a16 C++++>$ UB/L/X/*++++(+)>$ P+++(++++)>$
L+++(++) E W++(+) N+++(++) o? K? w---(-) O? M++ V? PS+() PE+(-) Y+
PGP? t+(+++) 5 X R? tv-(--) b++++(++) DI+ D+ G e->++++$ h! !r-- !y?
------END GEEK CODE BLOCK------
_______________________________________________
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.