views:

95

answers:

1

I've been scratching my head about this for the last 4 hours, trying out all kinds of little experiments, but I can't seem to figure out what's going wrong. Could this be a compiler bug?

Test.m:

- (id)initWithContentsOfURL:(NSURL *)aURL error:(NSError **)error
{
    if (!(self = [super init])) {
        return nil;
    }
    return self;
}

main.m:

NSError *error;

Test *t = [[Test alloc] initWithContentsOfURL:[NSURL fileURLWithPath:@"/"] error:&error];

Here's the compiler warning (from main.m):

warning: incompatible Objective-C types 'struct NSError **', expected 'struct NSDictionary **' when passing argument 2 of 'initWithContentsOfURL:error:' from distinct Objective-C type

I'm using the latest versions of Xcode and Snow Leopard.

+4  A: 
Phil Nash
You're correct. Objective-C doesn't like methods with the same signature to have different types (i.e. it doesn't do overloading), so avoid naming two methods the same if they take different parameters.
Graham Lee
That was it! Thanks! I had no idea Objective-C could run into these namespace pollution problems. You'd think Apple would have addressed this in Objective-C 2.0...
splicer
Yeah, it's a nasty one. I was bitten by it a while back: http://stackoverflow.com/questions/312608/why-do-i-need-to-cast-self-to-id
Phil Nash
Saying ObjC doesn't like methods with the same signature to have different types is like saying C doesn't like passing a `struct *` pointer to a function that takes a `void *` type. ObjC is just fine with methods with the same signature and different types (ie, different classes using the same selector but different types for the parameters). It does have a problem when you use `id` as the object type in such cases because it is ambiguous as to "which one you mean"- `id` means it can mean both. You need to statically type the object- `[(NSRightType *)[NSRightType alloc] init…]`
johne
I think Graham was probably thinking about trying to overload a method on the same class (which is not the case here).
Phil Nash