On Mar 21, 2008, at 4:07 PM, Stuart Malin wrote:
1) Do I just let -init dangle (so to speak)? and then if I ever forget that the object needs special initialization and instead call -init, the code will end up failing down the line somewhere where those initialization parameters matter...

2) Do I define a -(init) method and have it throw an exception? so that I'll find out right away if some code invokes -init rather than -initWithParams....

This is the approach I take when I care about non-designated initializers, except I log a message instead of throwing an exception (though maybe I should consider throwing an exception). I define the following macro:

#define DIGSLogNondesignatedInitializer()\
do {\
    {\
        if (DIGSGetVerbosityLevel() >= DIGS_VERBOSITY_ERROR)\
            DIGSLogError(\
                @"%@ -- '%@' is not the designated initializer",\
                [self class],\
                NSStringFromSelector(_cmd));\
    }\
} while (0)

(DIGSLogError() and DIGSGetVerbosityLevel() are other macros of mine.)

I put this macro in any init method that should not be called. For example:

- (id)initWithDatabase:(AKDatabase *)db
{
    DIGSLogNondesignatedInitializer();
    [self dealloc];
    return nil;
}

I considered putting the "[self dealloc];" and "return nil;" in the macro too, since they always appear after the DIGSLogNondesignatedInitializer() line. Not sure why I didn't do that -- maybe I will at some point.

3) Is there some way to tell the compiler to ignore the superclasses's -init for the subclass so that I get a compile time error? This would be my preferred outcome.

No, and you really wouldn't want the ability to subvert inheritance semantics this way. The nearest thing I know of to what you want is Java's approach to constructors. But -init is a regular method; there are no constructors in Objective-C.

--Andy

_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to