By A Beginner

Every Expert Was Once A Beginner

@property in Objective – C — June 28, 2015

@property in Objective – C

The keyword ‘@property’ in Objective-C can can be associated to a data member, to tell how the assessors can be automatically generated by the compiler. Which help us writing less code and saving the development time.  When the data member (instance variable) as a property ,it becomes available to the other classes to access or change its value using the accessors methods.

Declaring property

The properties can be declared in the @interface or the @implementation of the class with the appropriate attributes, but best practice is to declare them in the @interface of the class if it has to be accessed or changed by the other class. when declaring the property we can specify how the instance variables are exposed to other classes.

When you declare a property, you are telling other objects that getter and setter methods for accessing and changing one of the class’s instance variables exist.

Syntax for declaring the property in Objective-C:

@interface SomeClass : NSObject

@propert type name;
       //or
@property (attributes) type name;

@end

Property Attributes

Property attributes describe to other objects(and to the compiler) how the property behaves. The possible access specifiers are :

  • strong –   This property is a strong (owning) reference to an object. Using strong and weak properties controls whether the object referred to by the property stays in memory or not.
  • weak – This property is a weak (nonowning) reference to an object. When the object referred to by this property is deallocated, this property is automatically set to nil.
  • copy – This property’s setter copies any object passed to it, creating a duplicate object.
  • assign – This property’s setter method simply assigns the property’s variable to whatever is passed in, and performs no memory management.
  • readonly – This property does not generate a setter method, rendering the property read-only by other classes. (Your class’s implementation code can still modify the property’s variable, however.)
  • readwrite – This property generates both getter and setter methods. (This attribute is set by default—you need to explicitly use it only when overriding a superclass’s property.)
  • nonatomic – This property’s setter and getter do not attempt to get a lock before making changes to the variable, rendering it thread-safe.
  • getter=…, setter=… – Used when you want to change the default name of the accessors.                                               Eg:- @property (copy,getter=myGetter,setter=mySetter) NSObject *myObject;

 In a garbage-collected environment, retain is not different from assign. But in that case, the attributes __weak and __strong can be added.

Keywords @synthesize and @dynamic

@synthesize : This means that, unless developer already did it, the compiler should generate the accessors by it self, confirming to the constraints that were used in the property declaration. (With ARC all the properties are auto-synthesized and no need to explicitly synthesize the property explicitly in the @implementation)

@implementation A
@synthesize myProperty =_myProperty; //bind to "_myProperty" rather than "myProperty"
//(which does not even exist here)
@end

@dynamic : This means , it is up to the developer to provide the expected implementations of the accessor methods (a mere setter if read-only was specified when declaring the property; getter and setter otherwise).

If you do choose to mark a property as @dynamic, you need to implement the getter and setter methods yourself as shown below:

@implementation MyClass
@dynamic myProperty;
- (int) myProperty {
// this is the getter method for this property
      return 123;
}

- (void) setMyProperty:(int)newValue {
// this is the setter method for this property
}
@end

To understand more about @property refer:

  1. Encapsulating data