Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is strong identifier the default with @property statement?

Tags:

I'm learning Objective-C and the Cocoa Framework (via Aaron Hillgass' book) and trying to figure out why the following line includes the "strong" identifier.

@property (strong) NSManagedObjectContext *managedObjectContext;

As I understand it, strong is the default so why do I need to explicitly declare it?

like image 765
schmudu Avatar asked Jul 19 '12 00:07

schmudu


People also ask

Are Objective-C Properties strong by default?

strong (default) The default is called strong . Strong just means you have a reference to an object and you will keep that object alive.

What is @property in Objective-C?

The goal of the @property directive is to configure how an object can be exposed. If you intend to use a variable inside the class and do not need to expose it to outside classes, then you do not need to define a property for it. Properties are basically the accessor methods.

What is Ivar Objective-C?

Accessing an ivar through a getter/setting involves an Objective-C method call, which is much slower (at least 3-4 times) than a "normal" C function call and even a normal C function call would already be multiple times slower than accessing a struct member.

What is copy property?

"The copy attribute is an alternative to strong. Instead of taking ownership of the existing object, it creates a copy of whatever you assign to the property, then takes ownership of that. Only objects that conform to the NSCopying protocol can use this attribute..."


2 Answers

You can declare it without writing anything, But what happens when you come back to code or some other developer looks at your code?

You might have the knowledge that the default will be set to strong, but junior level programmer will get so confused to determine whether the declared variable is strong or weak.

like image 129
TeaCupApp Avatar answered Oct 24 '22 00:10

TeaCupApp


Agree with Richard.

//Strong and Weak References ARC introduces two new object reference qualifiers: strong and weak.

Under ARC, all object reference variables are strong by default. And this doesn’t apply to just properties; the default identifier with @property statement is assign for non-object types, for object type should be strong. all object references - property values, instance variables, automatic variables, parameter variables, and static variables - act like a retain property under ARC.

like image 21
Harry Zhang Avatar answered Oct 24 '22 01:10

Harry Zhang