• 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: semantics of init
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

  • Follow-Ups:
    • Re: checking for non zero before ... (Was Re: semantics of init)
      • From: Andrew Pinski <email@hidden>
References: 
 >semantics of init (From: Niko Matsakis <email@hidden>)

  • Prev by Date: Re: Interface Builder Palettes Collide
  • Next by Date: Re: checking for non zero before ... (Was Re: semantics of init)
  • Previous by thread: Re: semantics of init
  • Next by thread: Re: checking for non zero before ... (Was Re: semantics of init)
  • Index(es):
    • Date
    • Thread