Thanks Chris.

You are right on -- I had more than one method signature with the same, generic style name.

My rationale for departing from the canonical approach is because then I get a bit of extra type checking at compile time.

Given the canonical approach of returning id, one can define an interface for a class with an ivar:

        classA  *aIvar

and in the implementation, assign any sort of object to it.

        aIvar = [NSString stringWithString:@"OOPS"];

With my approach, when I define the interface for classA's initializer to be:

        - (classA*) - initClassA ......

then if I inadvertently assign an allocation to the wrong ivar:

        NSString        *s;

        // some code later ...

        s = [[classA alloc] init];

the compiler will issue a warning. With the canonical approach of the initializer returning type id, it wouldn't.

Now, I know this catches a puny amount of possible errors, but that is the reason why I deviated from the ordinary practice.

Given this, I think it would have been beneficial if

        [className alloc]

returned of the className type, rather than id. But I am certain there are numerous reasons against doing so, as otherwise it would likely have been implemented that way.

That aside, your suggestion to use protocol declaration for the delegate is something I had planned to investigate. Thanks for the further tips.


On Mar 11, 2008, at 10:42 AM, Chris Hanson wrote:

On Mar 11, 2008, at 1:19 AM, Stuart Malin wrote:

The interface for the XMPPStream initializer is:

        - (XMPPStream*) initWithDelegate:(id)initialDelegate;

The canonical return type of an -init method is (id). So the above should be:

        - (id)initWithDelegate:(id)initialDelegate;

You probably have several different -initWithDelegate: method signatures visible to the compiler at that point in your code, with different return types, and since +alloc also returns (id) it can't necessarily guarantee the right signature will be chosen for the expression. This is part of why Objective-C tends to avoid "generic" names like -initWith: or -initWithDelegate: in favor of slightly more verbose names that are less likely to overlap each other, for example -initWithStreamDelegate: for the above.

Also, once you've upgraded to Leopard and Xcode 3.0, with Objective- C 2.0 you can mark methods in protocols as @required and @optional, which is extremely useful for delegates because (for example) Xcode can perform better code completion than if you define your delegate via an informal protocol (a category on NSObject).

So the above could be:

- (id)initWithStreamDelegate:(id <XMPPStreamDelegate>) initialDelegate;

Hope this helps!

  -- Chris


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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