Question

I have a few doubts regarding the use of attributes. By reading the ARC transition document, strong and weak should be used in place of retain and assign.

However there are at least two cases where Xcode auto generated code is doing something else. I would like to know if this is just a 'still not updated' tool from Xcode, or if I am missing something.

The first case is with Core Data managed object auto generation.

After having created the relevant classes, this is what appears in a .h file:

@property(nonatomic,retain) NSString *myProperty;

what is the compiler doing in this case? Replace retain with strong?

But the strangest case (for my lack of knowledge) is with IBOutlet, the auto generated code when connecting Interface Builder outlet with .h file is this:

@property (unsafe_unretained, nonatomic) IBOutlet UILabel *myOutlet;

and again this seems different from the recommended 'weak' attributes. I mean recommended by users on various forum.

And this is added in viewDidUnload:

- (void)viewDidUnload {
    [self setMyOutlet:nil];
}

why do we need this statement? Even though by running the profiler instruments there's no trace of memory leak or other memory issue? I mean, without setting to nil everything works fine.

Was it helpful?

Solution

unsafe_unretained will store an unsafe pointer, i.e. the pointer will still be there when your label goes away, but it'll point to some garbage. That's why the -viewDidUnload code resets that pointer to nil at that point. Using weak properties will automagically turn the pointer value into nil when the object it points to goes away. That's why it's recommended.

retain and strong are basically the same thing. The object is retained, i.e. will not go away until the property is set to another value (e.g. nil) at which point the object is released.

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