views:

820

answers:

1

Hi all,

I am having trouble deciphering a "passing argument ... from distinct Objective-C type warning".

I have a constant string declared as:

extern NSString * const URL_1;

and defined as:

NSString * const URL_1 = @"http://someurl";

If I, say, assign that constant to an NSString as follows:

NSString *URL = nil;
...
URL = [[NSString alloc] initWithString:URL_1];

And pass this NSString as an argument to a function expecting an NSString:

ViewController *viewController = [[ViewController alloc] initWithURL:URL];

Function signature:

- (id)initWithURL:(NSString *)URL

I receive a warning that I am "passing argument 1 of 'initWithURL': from distinct Objective-C type"

As I understand it NSString objects are immutable once created, and I am assigning the value to the string once upon creation, so I don't understand why the constant nature of URL_1 should cause a problem.

I am sure I am being a donut here and have overlooked something simple! Please could someone help me resolve this warning? Many thanks in advance!

+2  A: 

There are many methods in the system frameworks that are declared as:

- (id)initWithURL:(NSURL *)anURL;

And, of course, +alloc is declared as:

- (id) alloc;

Thus, when the compiler sees:

ViewController *viewController = [[ViewController alloc] initWithURL:URL];

The return type of the allocation is id and the compiler is likely seeing the above declaration and that causes the warning. Now, generally, the compiler would also warn that it found multiple signatures for the selector -- multiple signatures for that particular method name.

If it isn't, it is quite likely because you haven't imported ViewController.h into the file that contains the above line of code.

In short, do not declare a method with the same name as another method that takes a different type of argument.

bbum
Great thanks bbum, I should've spotted that by the colour that the function call was displayed in.Changing both function call and variable name to initWithSourceURL and sourceURL respectively resolved the warning.
EddieCatflap
Excellent. I would also recommend changing the method to something like `initWithSourceURLString:` as ending w/URL usually means the parameter is an NSURL*
bbum