Beruflich Dokumente
Kultur Dokumente
■
■
■
■
alloc newObject mutableCopy retain
release
autorelease
■ autorelease release
myInstance
MyClass *myInstance = [MyClass createInstance];
■
■ retain
release autorelease
autorelease
{
Thingamajig *myThingamajig = [[Thingamajig alloc] init];
// ...
NSArray *sprockets = [myThingamajig sprockets];
// ...
[myThingamajig release];
}
alloc
release
release
retain
retain
release
autorelease
retainCount
autorelease NSObject
autorelease
sprockets
– (NSArray *)sprockets {
alloc
autorelease
autorelease
sprockets
– (NSArray *)sprockets {
sprockets
release
– (NSArray *)sprockets {
sprockets
– (NSArray *)sprockets {
arrayWithObjects:
sprockets
arrayWithObjects:
autorelease
release
autorelease
heisenObject
heisenObject
release autorelease dealloc
heisenObject
– (void)setMainSprocket:(Sprocket *)newSprocket {
[mainSprocket autorelease];
mainSprocket = [newSprocket retain]; /* Claim the new Sprocket. */
return;
}
setMainSprocket:
copy
– (void)setMainSprocket:(Sprocket *)newSprocket {
[mainSprocket autorelease];
mainSprocket = [newSprocket copy]; /* Make a private copy. */
return;
}
newSprocket mainSprocket
– (void)setMainSprocket:(Sprocket *)newSprocket {
if (mainSprocket != newSprocket) {
[mainSprocket release];
mainSprocket = [newSprocket retain]; /* Or copy, if appropriate. */
}
}
dealloc
0
dealloc
dealloc
dealloc
mainSprocket
auxiliarySprocket dealloc
- (void)dealloc {
[mainSprocket release];
[auxiliarySprocket release];
[super dealloc];
}
dealloc
dealloc
ClassName ** id *
NSError
■ initWithContentsOfURL:options:error: NSData
■ initWithContentsOfFile:encoding:error: NSString
■ executeFetchRequest:error: NSManagedObjectContext
NSError
dataFromPropertyList:format:errorDescription:
Document
page
don’t
Page retain retain
parent
paragraph
text
parent
NSTableView
NSApplication
setDelegate: nil
dealloc
dealloc
dealloc dealloc
dealloc
dealloc
■
1 0
alloc
1
alloc new
copy
1
copy
retain
1
release
1
autorelease
1
■
alloc newObject mutableCopy retain
■ dealloc
■ dealloc
dealloc
+className...
alloc
alloc
- (void)printHello {
NSString *string;
string = [[NSString alloc] initWithString:@"Hello"];
NSLog(string);
[string release];
}
- (void)printHello {
NSString *string;
string = [NSString stringWithFormat:@"Hello"];
NSLog(string);
}
- (void)printWindowTitle {
NSString *string;
string = [myWindow title];
NSLog(string);
}
retain release
retain release
- (NSNumber *)count {
return count;
}
retain
release
nil
[newCount retain]
- (void)setCount:(NSNumber *)newCount {
[newCount retain];
[count release];
// make the new assignment
count = newCount;
}
init
dealloc
init
- init {
if (self = [super init]) {
count = [[NSNumber alloc] initWithInteger:0];
}
return self;
}
initWithCount:
- initWithCount:(NSNumber *)startingCount {
if (self = [super init]) {
count = [startingCount copy];
}
return self;
}
dealloc
release
- (void)dealloc {
[count release];
[super dealloc];
}
NSNumber retain
release
- (void)reset {
NSNumber *zero = [NSNumber numberWithInteger:0];
[self setCount:zero];
}
- (void)reset {
NSNumber *zero = [[NSNumber alloc] initWithInteger:0];
[self setCount:zero];
[zero release];
}
- (void)reset {
NSNumber *zero = [[NSNumber alloc] initWithInteger:0];
[count release];
count = zero;
}
- (void)reset {
NSNumber *zero = [[NSNumber alloc] initWithInteger:0];
[self setCount:zero];
}
alloc
- (void)reset {
NSNumber *zero = [NSNumber numberWithInteger:0];
[self setCount:zero];
[zero release];
}
retain count
release autorelease
SIGBUS 10
NSMutableArray *array;
NSUInteger i;
// ...
for (i = 0; i < 10; i++) {
NSNumber *convenienceNumber = [NSNumber numberWithInteger:i];
[array addObject:convenienceNumber];
}
alloc release
convenienceNumber
NSMutableArray *array;
NSUInteger i;
// ...
for (i = 0; i < 10; i++) {
NSNumber *allocedNumber = [[NSNumber alloc] initWithInteger: i];
[array addObject:allocedNumber];
[allocedNumber release];
}
retain release
release dealloc
NSAutoreleasePool
autorelease release
release
autorelease release
autorelease
main
■
■
main()
main
main
void main()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[loopPool release];
}
exit (EXIT_SUCCESS);
}
for
NSAutoreleasePool
autorelease fileContents loopPool
loopPool
for fileName loopPool
autorelease
NSAutoreleasePool
NSAutoreleasePool
NSThread
NSThread
NSThread isMultiThreaded
autorelease
addObject:
release
autorelease
release
retain
autorelease
– findMatchingObject:anObject
{
id match = nil;
if (match != nil) {
[match retain]; /* Keep match around. */
}
[subPool release];
}
drain release
@property (copy) NSString *firstName;
@property (readonly) NSString *fullName;
@property (retain) NSDate *birthday;
@property NSInteger luckyNumber;
@synthesize firstName;
@synthesize fullName;
@synthesize birthday;
@synthesize luckyNumber;
nonatomic
- (NSString*) title {
return [[title retain] autorelease];
}
- (NSString*) title {
return title;
}
- (NSString*) title {
return title;
}
name
- (void)setName:(NSString *)aName {
[name autorelease];
name = [aName copy];
}
aName
name
- (NSString *)name {
return [[name copy] autorelease];
}
NSCopying copyWithZone:
NSCopying
copyWithZone: alloc init... NSCopyObject
■ NSCopying
- (void)setMyVariable:(id)newValue
{
[myVariable autorelease];
myVariable = [newValue copy];
}
- (void)setMyVariable:(id)newValue
{
[myVariable autorelease];
myVariable = [newValue retain];
}
- (void)setMyVariable:(id)newValue
{
myVariable = newValue;
}
@end
NSCopying
productName
delegate
productName
productName delegate
NSCopying
NSCopyObject alloc
init...
- (id)copyWithZone:(NSZone *)zone
{
Product *copy = [[[self class] allocWithZone: zone]
initWithProductName:[self productName]
price:[self price]];
[copy setDelegate:[self delegate]];
return copy;
}
NSCopying
NSCopyObject NSCopyObject
NSCell
copyWithZone:
- (id)copyWithZone:(NSZone *)zone
{
NSCell *cellCopy = NSCopyObject(self, 0, zone);
/* Assume that other initialization takes place here. */
cellCopy->image = nil;
[cellCopy setImage:[self image]];
return cellCopy;
}
NSCopyObject
copyWithZone: image
setImage:
- (void)setImage:(NSImage *)anImage
{
[image autorelease];
image = [anImage retain];
}
setImage: image
copyWithZone: image nil setImage:
image
alloc init... nil
cellCopy image
nil setImage:
NSCopyObject
NSSliderCell titleCell
- (id)copyWithZone:(NSZone *)zone
{
id cellCopy = [super copyWithZone:zone];
/* Assume that other initialization takes place here. */
cellCopy->titleCell = nil;
[cellCopy setTitleCell:[self titleCell]];
return cellCopy;
}
super copyWithZone
copyWithZone:
copyWithZone:
NSCopyObject
titleCell nil setTitleCell:
NSCopyObject
copyWithZone:
original 0xf2ae4 copy 0x104074 copy 0x104074
refCount
NSCopyObject refCount
copyWithZone: refCount copyWithZone:
NSCopyObject refCount
copyWithZone:
NSCopying
NSCopying
NSCopying
retain copyWithZone:
- (id)copyWithZone:(NSZone *)zone {
return [self retain];
}
NSMutableCopying
mutableCopyWithZone:
NSObject mutableCopy
mutableCopyWithZone:
CFString
NSString
dealloc
NSApp
dealloc NSApp
■ NSWindowController
NSDocument NSWindowController
NSWindowController
NSWindowController
instantiateNibWithOwner:topLevelObjects: NSNib
dealloc
NSWindowController
NSWindowController
setValue:forKey:
loadNibNamed:owner:options:
didReceiveMemoryWarning
didReceiveMemoryWarning
viewDidUnload
nil
- (void)viewDidUnload {
self.anOutlet = nil;
[super viewDidUnload];
}
viewDidUnload
nil setView:
- (void)setView:(UIView *)aView {
if (!aView) { // View is being set to nil.
// Set outlets to nil, e.g.
self.anOutlet = nil;
}
// Invoke super's implementation last.
[super setView:aView];
}
dealloc UIViewController
nil dealloc
- (void)dealloc {
// Release outlets and set outlet variables to nil.
[anOutlet release], anOutlet = nil;
[super dealloc];
}