Question

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?

Was it helpful?

Solution

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 localReferenceToObject you use in MyViewController into a @property(retain) propertyToObject; (or @property(strong) propertyToObject if you use ARC) and access it in your MyViewController.m with self.propertyToObject (instead of localReferenceToObject, 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 MyViewController instance 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

@interface MyViewController : UIViewController
// Here you write the public API in the .h / public header
// If you don't want your property to be visible, don't declare it there
@end

In your MyViewController.m file

@interface MyViewController ()
// This is the private API, only visible inside the MyViewController.m file and not from other classes
// Note the "()" to declare the class extension, as explained in Apple doc
@property(nonatomic, retain) MyCustomClass* referenceToObject; // Note: use strong (which is a synonym of retain) if you use ARC
@end

@implementation MyViewController
@synthesize referenceToObject = _referenceToObject; // not even needed with modern ObjC and latest LLVM compiler

- (id)initWithObject:(MyCustomClass *)obj
{
    self = [super init];
    if (self) {
        ...
        self.referenceToOjbect = obj;
        ...
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // do something with "self.refernceToObject"
}

// This memory management code is only needed if you don't use ARC
-(void)dealloc
{
    self.referenceToObject = nil; // release memory
    [super dealloc];
}

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 strong and not weak.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top