|
| [Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] |
There seems to be a strange competition in all the documents on how to
implement accessor methods.
- (void) setValue:(Something *)newValue {
[value autorelease];
value = [newValue retain]; // or copy, OK.
}
seems to be the most current.
However, I read that :
"autoreleasing an object is an expensive operation" etc...
(http://www.stepwise.com/Articles/Technical/MemoryManagement.html)
But am I sure that the instance variable is not used elsewhere ?
Because in that case I have to autorelease !
Then, the author proposes this code :
- (void) setTheory: (Theory *)newTheory
{
Theory *oldTheory = nil;
if ( theory != newTheory ) // If they're the same, do nothing
{
oldTheory = theory; // Copy the reference
theory = [newTheory retain];// First retain the new object
[oldTheory release]; // Then release the old object
}
return;
}
But I understand less the use of a swap variable. This code would be
more simple :
- (void) setTheory:(Theory *)newTheory {
if (theory == newTheory) return;
// [self willChange]; if necessary (I know nothing about that).
// but that's perhaps the reason of a so complicated manner ?
[theory release];
theory = [newtheory copy/retain];
}
So, what to do ?
| References: | |
| >Accessor methods and (auto)release <Memory trail> (From: Raphaël Delcroix <email@hidden>) |
| Home | Archives | Terms/Conditions | Contact | RSS | Lists | About |
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE
Contact Apple | Terms of Use | Privacy Policy
Copyright © 2011 Apple Inc. All rights reserved.