Simplest way to define @property and @synthesize in objective-c



What is @property ?

@property offers a way to define the information that a class is intended to encapsulate.

Means if we declare a variable or an object using @property then this variable or object can be accessible to other classes which importing its class.

Rule of declare an object using @property

if we declare an object using @property in the .h (header) file then we have to synthesise it using keyword @synthesize in the implementation file.

NOTE: By default compiler will synthesise accessor methods for above objects , where accessor methods are setter and getter.

For Example :

class name : 9to5iOSClass.h

@interface 9to5iOSClass : NSObject

@property (nonatomic,retain) NSString *str_name;

@end

class name : 9to5iOSClass.m

@implementation 9to5iOSClass

@synthesize str_name;

@end

In this example compiler with synthesise the accessor methods for the str_name

9to5iOSClass *object = [[9to5iOSClass alloc] init];

NSString *str_name1 = [object str_name];  // get the str_name

[object setName:@“9to5iOS”]; // here the str_name sets by setter method

List of attributes of @property

1. atomic  (default)

2. nonatomic

3. strong = retain (default)

4. weak = unsafe_unretained

5.retain

6. assign (default)

7. unsafe_unretained

8. copy

9. readonly

10. readwrite (default)



1. atomic

atomic means only one thread access the variable (static type).

atomic is thread safe( thread safe means at a time only one thread of the perticular instance of that class can have the control over that object)

It is slow in performance

atomic is default behaviour

atomic accessors in a non garbage collected environment means if used with retain/autorelease/release it will use a lock to ensure that another thread does not interfere with the correct setting / getting of the value.

atomic is not a keyword

For Example :

class name : 9to5iOSClass.h

@interface 9to5iOSClass : NSObject

@property NSString *str_firstname;  // its by default atomic

@property (atomic) NSString *str_name; // explicitly we make it atomic

@end

class name : 9to5iOSClass.m

@implementation 9to5iOSClass

@synthesize str_firstname;

@synthesize str_name;

@end



2. nonatomic

nonatomic is thread unsafe (means multiple thread can access the variable like dynamic type)

nonatomic is fast in performance

nonatomic is not default behaviour, we have to add nonatomic key in property attribute.

It have unpredictable behaviour if two or more different process (threads) access the same variable at a time.

For Example :

class name : 9to5iOSClass.h

@interface 9to5iOSClass : NSObject

@property (nonatomic) NSString *str_name;

@end

class name : 9to5iOSClass.m

@implementation 9to5iOSClass

@synthesize str_name;

@end

Explanation :

Suppose there is atomic string property called str_name,

if we call :

[self setName:@“9to5iOS”] from thread A

[self setName:@“iOS”] from thread B

[self str_name] from thread C

then all operation in different thread will be performed serially which means if one thread is executing setter or getter then other will wait, which make property str_name read/write safe but if another method calls

[str_name release] from  theard D

simultaneously then this operation might produce a crash because there is no setter / getter call involved here now.

So above examples show that an object is read/write safe (atomic) but not thread safe as another thread can simultaneously send any type of message to the object.

So if str_name is atomic , then invoking all thread A,B,C &D can produce unpredictable results.



3. strong

from iOS4 is a replacement of retain.

It comes with ARC

strong means keep this in the heap until i do not point to it anymore means i am the owner you cannot dealloc this

strong is needed when you need to retain object

Bydefault all instance variables and local variables are strong pointers.

generally used strong for UIViewControllers of UI item’s parents

strong is used  with ARC (ARC helps to maintain retain count and releases object for us when we are done with it.

For Example :

class name : 9to5iOSClass.h

@interface 9to5iOSClass : NSObject

@property (strong,nonatomic) ViewController *viewController;

@end

class name : 9to5iOSClass.m

@implementation 9to5iOSClass

@synthesize viewController;

@end



4.weak

iOS4 we can use as unsafe_unretained

keeps this as long as someone else points to it strongly.

Same as assign , no retain or release

weak means you don’t retain.

Generally used with IBOutlets (UIViewcontroller’s Childs), because child object only needs to be exists until or as long as the parent object does.

When the object is de-allocated the weak pointer is automatically set to nil

For Example :

class name : 9to5iOSClass.h

@interface 9to5iOSClass : NSObject

@property (weak,nonatomic) IBOutlet UIButton *btn_example;

@end

class name : 9to5iOSClass.m

@implementation 9to5iOSClass

@synthesize btn_example;

@end



5. retain

retain is required when the attribute is a pointer to an object.

the setter in this case automatically increase retain count of the object.

retain will auto converted or work like strong

For Example :

class name : 9to5iOSClass.h

@interface 9to5iOSClass : NSObject

@property (nonatomic,retain) NSString *str_name;

@end

class name : 9to5iOSClass.m

@implementation 9to5iOSClass

@synthesize str_name;

@end



6. assign

its the default behaviour

generate a setter which assigns the value to the instance variable directly rather then copy or retain it.

Generally used with the primitive types like NSInteger / CGFloat or objects like delegates.

For Example :

class name : 9to5iOSClass.h

@interface 9to5iOSClass : NSObject

@property (assign) NSInteger month;

@end

class name : 9to5iOSClass.m

@implementation 9to5iOSClass

@synthesize month;

@end



7. unsafe_unretained

unsafe_unretained is the ARC version of assign

unsafe_unretain is an ownership qualifier that tells ARC how to insert release or reatain calls.

Few class in Cocoa and Cocoa Touch that supports weak reference so we can not declare them as weak , these classes are NSTextView , NSColorSpace , NSFont etc. So we use unsafe_unretained for them.

Its similar to weak but its not set to nil when object is deallocated.

For Example :

class name : 9to5iOSClass.h

@interface 9to5iOSClass : NSObject

@property (unsafe_unretain) NSObject *unretainproperty;

@property (unsafe_unretain) NSString *str_name;

@end

class name : 9to5iOSClass.m

@implementation 9to5iOSClass

@synthesize unretainproperty;

@synthesize str_name;

@end



8. copy

with copy we cannot use retain

used when object is mutable

it specifies the new value should be sent -copy on assignment and the old value sent release

Use this if we need the value of the object as it is at this moment , and we do not want that value to reflect any changes made by other owners of the object.

For Example :

class name : 9to5iOSClass.h

@interface 9to5iOSClass : NSObject

@property (copy) NSString *str_name;

@end

No setter or getter method will be synthesized.

NSMutableString *str_name_set=[NSMutableString stringwithString:@“9to5iOS”];

9to5iOSClass2 *object = [[9to5iOSClass2 alloc] init];

object.name = str_name_set;

[str_name_set appendString :@“demo”];

str_name will remain unaffected



9. readonly

if we don’t want to allow the property to be changed using setter method we declare the property as readonly.

in this case compiler will generate a getter but don’t a setter.

For Example :

class name : 9to5iOSClass.h

@interface 9to5iOSClass : NSObject

@property (readonly) NSString *str_name;

@end



10. readwrite

it is the default behaviour

no need to specify this behaviour explicitly

It is opposite of readonly

class name : 9to5iOSClass.h

@interface 9to5iOSClass : NSObject

@property (readwrite) NSString *str_name;

@end



Strong & Weak Explanation, Thanks to BJ Homer:

Imagine our object is a dog, and that the dog wants to run away (be deallocated). Strong pointers are like a leash on the dog. As long as you have the leash attached to the dog, the dog will not run away. If five people attach their leash to one dog, (five strong pointers to one object), then the dog will not run away until all five leashes are detached. Weak pointers, on the other hand, are like little kids pointing at the dog and saying “Look! A dog!” As long as the dog is still on the leash, the little kids can still see the dog, and they’ll still point to it. As soon as all the leashes are detached, though, the dog runs away no matter how many little kids are pointing to it. As soon as the last strong pointer (leash) no longer points to an object, the object will be deallocated, and all weak pointers will be zeroed out. When we use weak? The only time you would want to use weak, is if you wanted to avoid retain cycles (e.g. the parent retains the child and the child retains the parent so neither is ever released).



One Comment

Add a Comment

Your email address will not be published. Required fields are marked *