@property definitions with ARC: strong or retain?

Objective CPropertiesAutomatic Ref-CountingNsmanagedobjectRetain

Objective C Problem Overview


Using Xcode 4.2 and ARC, I notice that the auto-generated code for an NSManagedObject still reads like this for properties:

@property (nonatomic, retain) NSString * someString;
  1. Shouldn't retain now be replace with strong or weak?

  2. Why does the auto-generated code still use retain

  3. What is the correct replacement for retain in this property statement?

I'm currently debugging a problem using NSFetchRequest, and I thought this might be the source of the problem. Thoughts?

Objective C Solutions


Solution 1 - Objective C

> 1) Shouldn't retain now be replace with strong or weak?

No. You cannot replace retain with weak; they are different. And strong is a 100% synonym for retain; they are identical. You can use either, so there is no "should" here. You can replace retain with strong if you like, but you don't have to.

> 2) Why does the auto-generated code still use retain

Why not? See (1). retain is correct so there is no problem.

> 3) What is the correct replacement for retain in this property statement?

There is no need to replace retain.

> I'm currently debugging a problem using NSFetchRequest, and I thought this might be the source of the problem. Thoughts?

It isn't.

Solution 2 - Objective C

To answer all three questions in one: retain and strong are synonymous with each other, so both are correct. The documentation states

> retain implies __strong ownership > > strong implies __strong ownership

Solution 3 - Objective C

Before ARC, you have to 'release' an object which is retained. That mean retain has counter part. After ARC you don't need to release. So use strong. Its a visual clue that you don't need to call release.

Solution 4 - Objective C

"retain" is equals to "strong".

"strong" is used for example:

@property (nonatomic, strong) NSString * someString;

And "__strong" is used for example:

-(void) someMethod
{
	__strong NSString* vStr = [[NSString alloc] initWithString:@"some string"];
}

On Apple Docs. says:

Property Attributes

The keywords weak and strong are introduced as new declared property attributes, as shown in the following examples.

// The following declaration is a synonym for: @property(retain) MyClass *myObject;
property(strong) MyClass *myObject;

Apple doc. http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionone09jasonView Question on Stackoverflow
Solution 1 - Objective CmattView Answer on Stackoverflow
Solution 2 - Objective CPhilipp SchlösserView Answer on Stackoverflow
Solution 3 - Objective Cuser2488343View Answer on Stackoverflow
Solution 4 - Objective CAlexView Answer on Stackoverflow