0
votes

I would like to know the correct approach here under ARC.

I have strong NSArray(custom class Objects of my own) @property inside a controller and when I am init another controller I have to pass just one NSString from these arrays to another controller's pointer which is weak. But I dont like this as I needed this be sorted

In main controller

    _leftPanelViewController.repotLabels = [[self.availableDashBoards sortedArrayUsingDescriptors:@[sortByReportLabel]]];

And the other controller has

    @property(nonatomic, strong)NSArray *repotLabels;

1) as I understood 1st returns weak pointer which I assign to another weak pointer so If code in mainController goes out of scope my reportLabel becomes "nil"?

2) So If I make reportLabels property (strong) this fix the problem but Is there any other approach? Should I also do something like this "copy" at the end?

    _leftPanelViewController.repotLabels = [[self.availableDashBoards sortedArrayUsingDescriptors:@[sortByReportLabel]]copy];
1

1 Answers

2
votes

I think there may be some confusion as to when the property's memory management options kicks in. They only affect the setter not the getter. Here's a quick review using ARC:

strong - The object is retained when setting the property. The previous value is released. This is what you want to use with objects most of the time as you usually want to have a retain on your ivars so they do no get deallocated Underneath, this looks something like this:

-(void)setObject:(id)obj
{
    [obj retain];
    [_object release];
    _object = obj;
}

assign - Always used for non-object values such as C scalars or structs. As these are just values and not objects, no memory management is done. Underneath, this looks something like this:

-(void)setInteger:(NSInteger)newInt
{
    _nteger = newInt;
}

copy - A new object of the same type is created and the contents of the object passed in are copied. This only works for objects that conform to the NSCopying protocol. You generally use this when you want to use the original values of an object and then edit the original object. (eg if you have an NSMutableString set to "Jane" and set a copied property using it, the property will still report "Jane" even after you change the originat NSMutableStringn to "John") Underneath, this looks something like this:

-(void)setObject:(id)obj
{
    id copy = [obj copy];
    [_object release];
    _object = copy;
}

weak - This is very similar to assign, except when the assigned object is dealloced, the pointer reverts to nil. This is generally used in situations that would result in retain cycles otherwise. For example, a view controller setting itself as the delegate of a view that it owns. Weak is used to avoid both objects retaining each other and thus never being released.

When you write object.property = value;, [object setProperty:value]; is actually being called behind the scenes.

I suspect that you want to use retain as you have listed. As far as I can tell, no weak properties come into play in the code snippets you have listed, unless _leftPanelViewController.repotLabels is a weak property.