is it OK to retain a variable passed by value in objective-c?
That is say:
- I have controller 1 I have a variable that I pass to controller 2
- it’s effectively passed by reference and I can use this when the use clicks on the controller 2 UINavigationController backbutton, in that the value will be modified in controller 1
- in controller 2 I have setup a ivar per the code below
Question – So is it OK for this controller 2 variable to be set using “Retain”, and also have it “released” and set to nil in the “dealloc” method? that is it won’t affect the object for controller 1? What happens for example if controller 2 is deallocated and the “release” and “=nil” is hit, will this affect the object as it’s still used by controller 1.
Controller 2 Code extract
@interface SelectorController : UITableViewController {
Config *_returnObject;
}
@property (nonatomic, retain) Config *returnObject;
@end
// implementation
@synthesize config;
- (void)dealloc
{
[config release]; config = nil;
[super dealloc];
}
Retaining and releasing the variable in controller 2 is more than OK, it’s how you should do it. 🙂
Setting the variable to
nilin the controller 2, will only set the variable to nil, not the stuff the variable points to, so your object will still be there for the variable in controller 1 to work with.