Friday 12 February 2016

Property Attributes in Objective C

Property attributes gives the functionality to define a way in which the property works. With attributes we can change the way property works. These property attributes can be categorized into three category.

1) Atomocity
       atomic and nonatomic
2)Access
       readonly and readwrite
3) Storage
      strong, weak , assign and copy


Atomic(default):
       An atomic property guaranteed that you will get a valid data when you try to read from it. It does not make any guarantees what the data might be but you will get the good data not just junk memory. This is used when multiple thread or processes pointing to same variable and reading and writing simultaneously.

Nonatomic:With it you lose the guarantee that you will get good data every time . When you try to access in the middle of the write, you could get back the garbage data.


ReadOnly: It makes the property read only that means no setter for it at all.

ReadWrite(default): It is the flip side of the readonly. Allows both read and write.

Strong(default): It means you have a reference to an object and you will keep the object alive. As you hold the reference to the object , the object will not  be deallocated and released back to the memory.

Weak: It gives the reference to the object so that  you can talk to the object but you can not keep it alive. If object's reference count goes to zero, the property becomes nil automatically.

Assign:It is used for primitives.

Copy:It works well with all kinds of mutable objects If you set a copy property, instead of just setting both references to the same object, what it actually does is makes a copy of the object you are setting and then sets the property to that copy.

No comments:

Post a Comment