Deigned Initializer - Where is my - (id)init?


#1

According to the rules for initializer (page 53) “Any other initializer a class has calls the class’s designed initializer”.
We defined the initializer initWithCoordinate, but we didn’t override the init method. It’s that ok? :slight_smile:


#2

The bullet after the one you quoted says “If a class declares a designated initializer that is different from its superclass, you must override the superclass’ designated initializer to call the new designated initializer”. This applies to Whereami MapPoint objects.

MapPoint is a subclass of NSObject. The designated initializer for NSObject is plain old init. Since MapPoint is changing the designated initializer, it should override init to call initWithCoordinate, using some default coordinate. This protects against someone instantiating a MapPoint object with just init. I guess in the context of Whereami, it doesn’t make sense to instantiate a MapPoint without a coordinate, so it’s probably not a big deal to omit an overridden init. However, in the interest of good programming style, and to protect against future reuse with further subclassing, init should be overridden.

Is my understanding consistent with others here?


#3

Yes, your understanding is correct. This chapter really should include the override to init. I’m always weighing the cost of “oh god, here’s more typing” vs. reinforcing a pattern that has been taught.

A good programmer would override init as you have stated.