Properties in Objective-C
Property is a good feature of Objective-C programming language. That allows you to generate the setter and getter methods for your instance variable. In old version of Objective C, that means in the Objective -C 1.0 we need to specify the getter and setter method. In the version 2.0 apple introduced the properties in the Objective- C. By enabling the property for a instance variable we can access the getter and setter of that instance Variable. To get and set the values for the instance variable, we can use the dot operator. For example, if we have defined property for the NSString Object, for better understanding i am going to name the NSString object as stringObject. if thestringObject has a property name, then you can write the name like this
Property is a good feature of Objective-C programming language. That allows you to generate the setter and getter methods for your instance variable. In old version of Objective C, that means in the Objective -C 1.0 we need to specify the getter and setter method. In the version 2.0 apple introduced the properties in the Objective- C. By enabling the property for a instance variable we can access the getter and setter of that instance Variable. To get and set the values for the instance variable, we can use the dot operator. For example, if we have defined property for the NSString Object, for better understanding i am going to name the NSString object as stringObject. if thestringObject has a property name, then you can write the name like this
The Compiler can translate the object to like this :
we use @property directive in the class declaration to declare a property. After writing the @property directive we need to @synthesize that property instance in the Definition part(ie in the implementation). You Can use the getter and setter in different ways. we can use many attributes to generate different kind of setter and getter method in objective-C. next i am going to explain about this.
Property Declaration
In Objective-C the property declaration is done after the instance variable area and before the method declaration area in the definition class(in the .h file).
it will be look like this :
After declare the property in the declaration class(.h file), next step is to synthesize the property in the implementation file(.m file). After synthesize the property it will enable the setter and getter methods for that instance variable. It will look like this
we can generalize the property declaration step like this
Property Attributes
The property attributes are used to influence how the compiler generates the getter/setter methods.
we can use following Property Attributes in Objective-C :
1. atomic: “atomic”, the synthesized setter/getter will ensure that a whole value is always returned from the getter or set by the setter, regardless of setter activity on any other thread
2. nonatomic: “nonatomic”, just opposite to the atomic behavior.nonatomic is considerably faster than “atomic”. For more clarity , by using the nonatomic attribute, the compiler does not need to generate extra code for the tread safety. if you not specified the nonatomic the compiler will generate extra code.
3. readonly: “readonly”, this attribute tell the compeller that the property can only read, it is not possible to write.
4. readwrite: “readwrite”, this is by default, both setter and getter will be generated.
5. assign : “assign”, it will help us to set the value to the property directly. that means you can directly assign the value to the instance variable.
there are lots of other Property Attributes in Objective-C. That will describe in a separate article.
stay tuned….
No comments:
Post a Comment