Properties is the feature of the Objective C .
It reduces lots of code writing.
Its mainly used for accessor i.e. getting and setting data member. compiler will add that code of getter and setter with respect to the thread safe or not.
syntax
.h file
It reduces lots of code writing.
Its mainly used for accessor i.e. getting and setting data member. compiler will add that code of getter and setter with respect to the thread safe or not.
syntax
.h file
#import <Foundation/Foundation.h>
@interface Circle : Shape {
int color;
int radius;
}
@property int color;
@property int radius;
@end
.m file
#import "circle.h"
@implementation Circle
@synthesize color;
@synthesize radius;
//here code of implementation
@end
in above example for color and radius getter and setter will be added by compiler.
@property does work of declaration of setter and getter.
@synthesize does work of creating implementation of setter and getter.
u can acces these data member with dot operator outside.
ex
circle.radius=10; //setter method will be get called sometime these setter can be thread safe.
We saw uses of property with simple data type.
If u want to use with class objects then its better to do with some extra attributes.
ex
@property (retain) Rect *circleorigin;// retain means at the time of setting method is written by compiler object will be retained .
same way there are some attrinute:
1.nonatomic:- when u do not want that data member should not be thread safe . faster access.
2.copy:- objects copy will be get created.
3.assign:- if u do not want to retain that object.
4.readwrite:- we can read and write values to data member (
By default, properties are mutable).
By default, properties are mutable).
5.readonly:- for ready only properties. generate getter. not require setter.
Suppose u want the internal name and property name should be different.
create data member inside class declaration and use as below
@synthesize color=circlecolor;
inside setter method circlecolor value will be get set. This is useful normally when u are referencing that data member inside class should be accessible without self.
By default when u want to access that data member anywhere u have to access with either objectname.datamember or inside class self.datamember.
0 comments:
Post a Comment