Skip navigation.
 
mlRe: Best practice for overridden initializers in subclasses
FROM : Quincey Morris
DATE : Wed Mar 26 00:06:03 2008

On Mar 25, 2008, at 15:01, Andy Klepack wrote:

> I have a subclass of NSObject that provides its own designated 
> initializer that allows client code to configure an instance with 
> initial values. Instances of the class itself are immutable. At the 
> same time, instances where no initial values are supplied do not 
> make conceptual sense.
>
> I'm wondering how to deal with overriding the 'init' method of 
> NSObject. There's really no sensible default values that I could 
> have init pass along to my designated initializer. It doesn't make 
> sense for clients to call 'init' and I'm debating whether to return 
> nil, throw some sort of exception, make the instance 'dead' and 
> essentially do nothing, or to do something else..
>
> Anyone have a recommendation for the best practice in this case?
> -Andy


I don't know about best, but the *shortest* is:

- (id) init {
   [super init];
   return nil;
}

or (if you're so retro as to still be doing retain/release):

- (id) init {
   [[super init] release];
   return nil;
}

If you're not writing the client code yourself, throwing an exception 
might be a bit more useful, though.

Related mailsAuthorDate
mlBest practice for overridden initializers in subclasses Andy Klepack Mar 25, 23:01
mlRe: Best practice for overridden initializers in subclasses Quincey Morris Mar 26, 00:06
mlRe: Best practice for overridden initializers in subclasses Andy Lee Mar 26, 00:26
mlRe: Best practice for overridden initializers in subclasses Kyle Sluder Mar 26, 00:38
mlRe: Best practice for overridden initializers in subclasses Quincey Morris Mar 26, 01:09
mlRe: Best practice for overridden initializers in subclasses Chris Suter Mar 26, 01:42
mlRe: Best practice for overridden initializers in subclasses Andy Lee Mar 26, 04:47