A little trick

Most subclasses add instance variables. Objects initialized using initializer methods inherited from superclasses will only be partially initialized. Therefore you should override all initializers to avoid accidentally creating a partially initialized object. However unless there is a reasonable default for all instance variables this may not be possible. In such cases, one usually overrides the initializer by a method returning nil. However it is difficult to debug nil-errors. A better solution is to override the method to invoke doesNotRecognizeSelector: Since _cmd is always the current method’s selector one can use the same method body. For instance:


- (id) init
{ [self doesNotRecognizeSelector:_cmd]; return nil; }

One Response to “A little trick”

  1. Joel Bernstein Says:

    Pity it can only really be checked at runtime…

Leave a Reply