80

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?

Gabriele Petronella
  • 102,227
  • 20
  • 204
  • 227
one09jason
  • 1,871
  • 2
  • 14
  • 14

4 Answers4

110

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.

matt
  • 447,615
  • 74
  • 748
  • 977
  • 9
    I'd posit though, for a community as obsessed (and rightly so!) with standards and compliance to Apple Way(s), seeing retain in only synthesized models is subpar. I change to strong just to be pedantic. For one, makes ack-ing a large project for improper syntax a bit easier, amongst a half dozen other little things. – Eric Goldberg Dec 25 '11 at 06:35
  • 12
    Not sure exactly what emotional architecture is. Can you provide an example in the "cold, clear light of day?" ;-) false and NO are synonyms, but we use NO by convention in Objective-C. There are reasons for being consistent; to be inconsistent is fine, but the burden of proving worthiness is on the one breaking convention, not the one adhering to it. – Eric Goldberg Dec 26 '11 at 09:11
  • 9
    With all due respect, @EricGoldberg does have a point: convention matters. And because you're going to be `__strong` in other places in your code (there is no `__retain`) you might want to switch to using `strong` for consistency. This is similar to indenting your code in a consistent fashion, even though the code will run fine without this. – Dan Rosenstark Dec 28 '11 at 02:15
  • @Yar - I don't disagree. In my own code I have in fact replaced retain with strong everywhere. But this is for aesthetic reasons. The fact remains that what I said is right: strong and retain are absolutely interchangeable. The OP's question was whether retain *should* be replaced by *strong*. My answer was: no. Of course he *may* do so, and most people *will* do so. But as Hume showed, you can't get from "is" to "ought". :) – matt Dec 28 '11 at 02:59
  • That wasn't the question. The question was whether leaving retain as retain under ARC could be the source of the problem. I don't see how that could be. – matt Dec 28 '11 at 03:20
  • 3
    @matt: if you look at my original comment, the one where you accused me of coding "emotionally" (whatever that is), you'll see that I use the words "posit" and "just to be pedantic." I in fact suggest "ought" and not "is." – Eric Goldberg Dec 28 '11 at 07:17
  • 1
    ALL OF PROGRAMMING is about idiom. There are no other issues. All programming is a synonym, the whole engineering practice of programming is about using the current idiom, so as to add value to the code for the client. – Fattie Jan 22 '14 at 17:19
  • It should be remembered though that Assign and Weak are not the same, as Assign can actually cause you to crash an app where weak will be handled by ARC properly. For example check out the UIWebView Delegate and see how you have to nil out the delegate otherwise the app will crash. – Darxval Apr 02 '14 at 21:43
40

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

Community
  • 1
  • 1
Phlibbo
  • 5,073
  • 2
  • 36
  • 52
  • Thanks for the response. I did read the Apple document on ARC. I do understand that retain translates to strong. However, this does not explain why the code that is automatically generated by Xcode when create new NSManagedObject(s) will give you @property (nonatomic, retain) – one09jason Oct 17 '11 at 21:37
  • 3
    Unless I am missing something here, that explains it perfectly. The code that Xcode creates is correct since retain works just as strong, so where is your question? – Phlibbo Oct 17 '11 at 21:40
  • 6
    If you run the ARC refactoring on your project, it will convert all those `retain`s to `strong`s. So my impression is that `strong` is the preferred attribute, but the NSManagedObject generator has not been updated. But that's just a guess; maybe no one at Apple considers the distinction important. – theory Oct 23 '11 at 07:18
4

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.

3

"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

Alex
  • 235
  • 3
  • 6