xcode 找到多个名为“标签”的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8817439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple methods named 'tag' found
提问by Diffy
why do I get this warning in my code below:
为什么我在下面的代码中收到此警告:
- (IBAction)shareThisActionSheet:(id)sender
{
int row = [sender tag]; //warning is here! Multiple methods named 'tag' found
...
回答by justin
Description
描述
The problem is that the compiler sees more than one method named tag
in the current translation unit, and these declarations have different return types. One is likely to be -[UIView tag]
, which returns an NSInteger
. But it's also seen another declaration of tag
, perhaps:
问题是编译器tag
在当前翻译单元中看到了多个命名的方法,并且这些声明具有不同的返回类型。一个很可能是-[UIView tag]
,它返回一个NSInteger
。但它也看到了另一个声明tag
,也许:
@interface MONDate
- (NSString *)tag;
@end
then the compiler sees an ambiguity - is sender
a UIView
? or is it a MONDate
?
那么编译器看到的歧义-是sender
一个UIView
?或者是一个MONDate
?
The compiler's warning you that it has to guesswhat sender
's type is. That's really asking for undefined behavior.
编译器警告您它必须猜测sender
的类型是什么。这真的是在要求未定义的行为。
Resolution
解析度
If you know the parameter's type, then specify it:
如果您知道参数的类型,则指定它:
- (IBAction)shareThisActionSheet:(id)sender
{
UIView * senderView = sender;
int row = [senderView tag];
...
else, use something such as an isKindOfClass:
condition to determine the type to declare the variable as before messaging it. as other answers show, you could also typecast.
否则,使用诸如isKindOfClass:
条件之类的东西来确定声明变量的类型,就像在消息传递之前一样。正如其他答案所示,您也可以进行类型转换。
回答by Bastian
The problem is that sender
is defined as a (id)
object. At compile time xcode does not know what kind of objects will get passed to your function.
问题是它sender
被定义为一个(id)
对象。在编译时 xcode 不知道什么样的对象会被传递给你的函数。
If you write this function for a specific object type, you could you could write for example
如果您为特定对象类型编写此函数,您可以编写例如
- (IBAction)shareThisActionSheet:(UIButton*)sender
or you could hint the compiler the type of object with the call
或者你可以通过调用提示编译器对象的类型
int row = [(UIButton*)sender tag];
回答by Stas
Bastian is right you should convert your sender to button like this:
巴斯蒂安是对的,您应该像这样将发件人转换为按钮:
UIButton * button = (UIButton *)sender;
int row = button.tag;