Skip to main content

Posts

Showing posts from January, 2015

Deep Diving Objective-C Properties (Part 2)

Previous post of properties explained about the lifetime qualifiers. Today post will have exciting stuff about the other qualifier and some concepts about where to used these qualifier. Threads and Properties: Suppose you are working in multithreaded environment and two thread want to access the same property at same time. Thread A want to write and thread B is in middle of reading the property name.  What happen which value thread B will get. ? There are two qualifier which decide the behaviour in such condition. 1)  atomic (Default) 2) nonatomic Atomic: 1) Default behaviour. 2) Ensure that one thread will access property or method at a time. (Inconsistency will be not there as atomicity). Thread B will get autorelease value always in the above case. 3) Does not ensure the thread safety of instance variable. Any other thread can access the instance variable bind to property. So instance variable in not thread safe only setter and getter is thread safe.

Deep Diving Objective-C Properties (Part 1)

Properties Properties in objective c are a convenient way to provide the accessors (getter and setter) for instance variable in objective - c language. Let suppose you have a mobile then what are its properties? Name: Brand Name: Screen Size: etc. So properties define the things. In a similar manner properties in objective - c define the class or objects. A Simple Example for a Person class with the basic attribute. Example:  @interface  Person :  NSObject @property   NSString * name; @end Compiler will generate 
a getter and setter for you with the following convention: - ( void ) setName:( NSString *)name{     //Compiler generated the code for getter property } - ( NSString *)name{         //Compiler generated the code for getter property } From the above code there are some questions raised in mind? 1) What is the name of instance variable in the above case? 2) How