Friday, 5 June 2015

Atomic And nonAtomic Properties


Object Oriented Concept in Objective C

Classes and Objects : 
Classes is collection of similar type of data.In general you can say that classes are blueprint of data. Classes does not define any Data. 
So there require things to print data these are objects. Objects are the instances which define the instance variables andoperation on that data.
Example:As you were study in any computer class with group of students.
here Student is object. Your computer class is your real class.
Syntax of Classes: 
Here  name of class is Student.
You can define values in classes in the interface.
@interface student :NSObject
{
NSInteger *StudentID;  //instance variables
NSString *StudentName;
}
-(void)printStudentName:(NSInteger *)StudentID StudentName:(NSString *)StudentName;

Creating and allocating space to object
Student *student1=[[Student alloc]init];
// above way store your data in heap;
student1.StudentID=@"A101";
student1.StudentName=@"Suraj";

There is another way by doing
Student *student1=student1.StudentName;
// It stored value in stack.

But we use Properties in objective C.
Properties are backed up by instance variables.
Why we use properties instead of instance variables

There is need to setup getter and setter method for instance variables. But  setter method name and instance variables name are same causing big problem in case of multiple instance variables case So there is need to set it up with properties.

Properties Properties are used to describe the property of an Object.
These are declared with

@property NSString *content;

@Property(nonatomic,strong) NSString *content;

Strong property  The value of properties are stored in heap. It will not release memory unless or until anyone or it free up from the memory.
It does not create garbage collection due to automatic reference counting.

Weak Property  If there is weak pointer and strong pointer then strong pointer is prefer first and weak pointer is set to null. IT does not crashes the application.

Atomic It is used in case of thread safety by making locking techniques between different threads.Also it applied operation to temperaory location then to permanent location of memory.

Nonatomic  It is opposite to atomic case of property.

Inheritance Reusability of code needed then use of inherit the classes occurs.

Lets look up example
I have used instance variables for simplicity but you need to use property.
For Student class:

@interface student :NSObject
{
NSInteger *StudentID;  //instance variables
NSString *StudentName;
}
-(void)printStudentName:(NSInteger *)StudentID StudentName:(NSString *)StudentName;

@implementaion Student
-(void)printStudentName:(NSInteger *)StudentID StudentName:(NSString *)StudentName
{
NSLog(@"The %@ has StudentID is:%@",StudentName,StudentID);
}
@end

For Teacher Class

@interface Teacher:Student
{
NSString *TeacherName;
}
-(void)printStudentName:(NSInteger *)StudentID StudentName:(NSString *)StudentName TeacherName:(NSString *)TeacherName;


@implementation Teacher
-(void)printStudentName:(NSInteger *)StudentID StudentName:(NSString *)StudentName TeacherName:(NSString *)TeacherName
{
NSLog(@"%@ has StudentId:%@ is studied by The Teacher:%@",StudentName,StudentID,TeacherName);
}

I hope you like it.
If you want to go for brief description of property then click on this link...
Brief description of property

Polymorphism And Data Encapsulation









No comments:

Post a Comment