Posts

Showing posts from January, 2014

Designated Initializers in Objective - C

Object construction in Objective C is a two phase procedure:---> 1.  First the object has to be allocated in memory, hence the ‘alloc’ message we always see when a object is invoked this way (and not for example via a ‘get…’, which would be a Factory): 2. Second phase involves object initialization which is quite similar to what would be a constructor in C++. When we implement method ‘init’ (which is just a convention) we have to take care on superclass designated initializer .   MyObject* obj1 = [[MyObject alloc] init]; Designated initializer :-->   Designated initializer is the method that best set up our object between all initializer methods. - ( id ) init: - ( id ) initWithColor:( NSColor *)color; - ( id ) initWithColor:( NSColor *)color andSize:( NSInteger )size; What method do you think is the most complete? Our third method includes two parameter while the other only one or none. Now, we got a pattern that you are going to see every time you