开发者

Help deciphering an NSString "passing argument ... from distinct Objective-C type warning"

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:

- (i开发者_StackOverflowd)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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜