Brief Description of Properties In Objective C
Before going to the depth of Object Oriented World First we
go through brief explanation of property in objective c. In general Properties
are used to encapsulate the data behind the instance variables. Encapsulation
means just way of data hiding.
You can make the property
readonly,readwrite,retain,assign,copy,strong,weak,atomic,non-atomic,getter=method,setter=method.
Example:
In .h file:
@interface Student
@property NSInteger *StudentID;
In .m (implementation) file:
@implementation Student
Student *student1=[[Student alloc]init];
Do you know that getter method are those which return a
value each time when it is called.
-(NSInteger)StudentID
{
Return _StudentID; //return value of instance variables
}
-(void)setStudentID:(int)SID
{
_StudentID=SID;
}
@synthesize StudentID=_StudentID;
@Synthesize Property=_Instance variable;
// you can call with same name as property name but you can
use the different name for instance variable but that is not the good idea.
Xcode compiler create automatically getter and setter method
for you.
NSLog(@”The Student ID is:%@”,student1.StudentID);
Now we go through ReadOnly property
@property(readonly) NSString *StudentID;
Readonly means property will not include setter method or
Setter method does not work.
(NSInteger)StudentID
{
Return 12;
}
How the Properties are called?
Properties are called by dot syntax.
Example:
Object.PropertyName
Student1.StudentID=101;
NSInteger *integer=student1.StudentID;
You can print integer by using NSLog function.
When you try to set value with dot notation then it shows
the compile error.
ReadWrite is By default you do not need to specify
explicitly or if you write by fault then its meaning is same.
@property(readwrite) NSString *StudentName;
@property NSString *StudentName; //It is equivalent to above
statement.
Retain property
When there requires save of old values then there is need
for retain case property.
@property(retain) NSString *StudentID;
You need to only change the setter method that is
-(void)setStudentID:(NSString *)StudentID
{
If(oldstudentid==StudentID)
{
Return ;
}
Student1=oldstudentid;
oldstudentid=[StudentID retain];
[oldstudentid release];
}
If the old value is equal to same new value then first value
release the object before oldstudentid retain it.
Copy Property
When you want to set NSMutable value use copy accessor then
there shows warning regarding setter method
Example:
@property(copy) NSString *StudentName;
NSMutableString *studentname=@”suraj”;
Student1.StudentName=studentname;
Above shows unaffected to the studentname property there
need change in setter method.
Assgin property
Assign property stores the value directly to non-pointer
attribute rather then retain or copy the attributes.
@property(assign) NSInteger yearofjoiningcollege;
It is generally used for primititive types like integer and
float.
Getter=method
If you want to get properties with different method names then use this attributes.
@property(getter=isStudying) bool *studentStudy;
Setter=method
It is opposite to the getter=method.
Appsinvo is the Top Mobile App Development Company in India to deliver end-to-end services and solutions to the clients.
ReplyDeleteMobile App development company in Asia
Mobile App Development Company in Europe