Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple methods named 'tag' found

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
    ...
like image 992
Diffy Avatar asked Jan 11 '12 10:01

Diffy


2 Answers

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:

@interface MONDate
- (NSString *)tag;
@end

then the compiler sees an ambiguity - is sender a UIView? or is it a MONDate?

The compiler's warning you that it has to guess what sender's type is. That's really asking for undefined behavior.

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.

like image 94
justin Avatar answered Nov 12 '22 15:11

justin


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.

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]; 
like image 5
Bastian Avatar answered Nov 12 '22 14:11

Bastian