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?
strong (default) The default is called strong . Strong just means you have a reference to an object and you will keep that object alive.
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.
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.
"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..."
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With