帮助破译 NSString“从不同的 Objective-C 类型警告传递参数...”
我无法破译“传递参数......来自不同的 Objective-C 类型警告”。
我有一个常量字符串声明为:
extern NSString * const URL_1;
并定义为:
NSString * const URL_1 = @"http://someurl";
如果我将该常量分配给 NSString,如下所示:
NSString *URL = nil;
...
URL = [[NSString alloc] initWithString:URL_1];
并将此 NSString 作为参数传递给需要 NSString 的函数:
ViewController *viewController = [[ViewController alloc] initWithURL:URL];
函数签名:
- (id)initWithURL:(NSString *)URL
我收到一条警告,指出我“传递'initWithURL'的参数1:来自不同的Objective-C类型”
据我了解,NSString对象一旦创建就是不可变的,并且我在创建时将值分配给字符串一次,所以我不明白为什么恒定的性质URL_1 应该会导致问题。
我确信我是个甜甜圈,忽略了一些简单的事情!请问有人可以帮我解决这个警告吗?非常感谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
系统框架中有很多方法被声明为:
当然,
+alloc
被声明为:因此,当编译器看到:
分配的返回类型是
id< /code> 并且编译器可能会看到上述声明并导致警告。现在,通常编译器还会警告它发现了选择器的多个签名——该特定方法名称的多个签名。
如果不是,很可能是因为您尚未将
ViewController.h
导入到包含上述代码行的文件中。简而言之,不要声明与另一个采用不同类型参数的方法同名的方法。
There are many methods in the system frameworks that are declared as:
And, of course,
+alloc
is declared as:Thus, when the compiler sees:
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.