I’m developing an app in Objective-C using ARC.
My simplified code looks like this:
ClassA (.m)
MyCustomClass *obj = [[MyCustomClass alloc] initWithValue1:@"abc" value2:1000];
MyViewController *vc = [[MyViewController alloc] initWithObject:obj];
// "vc" will become the first item of a UITabBarController
MyViewController (.h)
- (id)initWithObject:(MyCustomClass *)obj {
...
localReferenceToOjbect = obj;
...
}
- (void)viewWillAppear:(BOOL)animated {
// do something with "localRefernceToObject" <---
}
launching the app will result in a call to a zombie: when the ViewController is shown, the “obj” will be already deallocated and so i can’t use it anymore.
my workaround is:
ClassA (.h)
@interface ClassA : UIViewController {
MyCustomClass *obj;
}
ClassA (.m)
obj = [[MyCustomClass alloc] initWithValue1:@"abc" value2:1000];
MyViewController *vc = [[MyViewController alloc] initWithObject:obj];
// "vc" will become the first item of a UITabBarController
is this the right way?! i don’t think so: why i’ve to store an istance of an object that is useless for ClassA?
i can’t get an explanation on what’s actually happening. could you help me?
You’re right in the fact that it is not logical to keep around a reference to obj in ClassA.
But if you need to keep around the reference to obj for MyViewController to use it, retain it in MyViewController, not in ClassA, because that’s MyViewController that will use it.
The easiest way to do this is to transform your
localReferenceToObjectyou use inMyViewControllerinto a@property(retain) propertyToObject;(or@property(strong) propertyToObjectif you use ARC) and access it in yourMyViewController.mwithself.propertyToObject(instead oflocalReferenceToObject, to be sure to call the property’s setter and thus really retain the object).This way, the object will be retained and kept around while your
MyViewControllerinstance is still alive.[EDIT] If you want this property to be private, you can declare it in the class extension so that it is not accessible from other classes, as in the below example. See here in Apple’s documentation for more details.
In your MyViewController.h header file
In your MyViewController.m file
Personally, as suggested by Apple in some WWDC sessions, I now really rarely use instance variables and prefer the use of properties instead, either public in the .h or private in the .m.
If you use ARC, you can still use an instance variable instead of a property as ARC will retain it for you, but as long as you make sure your instance variable is declared as
strongand notweak.