Home > DeveloperSection > Forums > Difference between atomic, nonatomic @property declarations
Anurag Sharma
Anurag Sharma

Total Post:40

Posted on    November-15-2015 11:11 PM

 iPhone iPhone  Ios  Objective C 

 1 Reply(s)
 343  View(s)
Rate this:

I have a confusion in @property declaration between atomic & nonatomic?
For example:

@property(nonatomic, retain) UITextField * countryName;
@property(atomic, retain) UITextField *countryName;

What is the operational difference between both statements?


Tarun Kumar

Total Post:210

Posted on    November-17-2015 10:57 PM

Here is the difference:
- atomic means only one thread access the variable(static type).
- atomic is thread safe but it is slow in performance
- atomic is default behavior
- atomic accessors in a non garbage collected environment will use a lock to ensure that another thread does not interfere with the correct setting/getting of the value.
- it is not actually a keyword.
@property(retain)NSString *title;
@synthesize title;

- nonatomic means multiple thread access the variable(dynamic type).
- nonatomic is thread unsafe.
- but it is fast in performance
- nonatomic is NOT default behavior, we need to add nonatomic keyword in property attribute.
- it may result in unexpected behavior, when two different process (threads) access the same variable at the same time.
@property(nonatomic,retain)NSString *title;
@synthesize title;

Don't want to miss updates? Please click the below button!

Follow MindStick