objective-c 击败“多个名为 'xxx:' 的方法找到”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1038171/
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
Defeating the "multiple methods named 'xxx:' found" error
提问by Holtorf
In my current project inside the file ViewController.m, I am running the method:
在文件 ViewController.m 中的当前项目中,我正在运行该方法:
[[connection writer] writeData: data];
It returns the warning:
它返回警告:
warning: multiple methods named 'writeData:' found
I am attempting to call the method:
我正在尝试调用该方法:
- (void) writeData: (NSData*)data
...in TCPWriter.m. Unfortunately, there are two other writeDatamethods
...在 TCPWriter.m 中 不幸的是,还有另外两种writeData方法
- (void)writeData:(NSData *)data;
...in NSFileHandle.h and...
...在 NSFileHandle.h 和 ...
- (BOOL)writeData:(NSData *)data
...in NSURLHandle.h. This is especially confusing to me because [conn writer]should return the TCPWriterclass and that class should call the correct writeDatamethod. Furthermore, I am not even completely sure that NSFileHandle.h and NSURLHandle.h are even included in any of the libraries included in ViewController.h, rather than in a different part of the project.
...在 NSURLHandle.h 中。这让我特别困惑,因为[conn writer]应该返回TCPWriter类并且该类应该调用正确的writeData方法。此外,我什至不能完全确定 NSFileHandle.h 和 NSURLHandle.h 是否包含在 ViewController.h 中包含的任何库中,而不是包含在项目的不同部分中。
How can I show the compiler which writeDatamethod I want to call and why does this error happen?
如何向编译器显示writeData我要调用的方法以及为什么会发生此错误?
回答by Peter N Lewis
Make sure [connection writer] is actually returning a TCPWriter*. If it is returning an id, then the compiler will not know which writeData to use. Also, make sure you are importing the TCPWriter.h file - if the compiler does not see the header files, it will default to returning id, which will get you back to the same problem.
确保 [connection writer] 实际上正在返回 TCPWriter*。如果它返回一个 id,那么编译器将不知道要使用哪个 writeData。另外,请确保您正在导入 TCPWriter.h 文件 - 如果编译器没有看到头文件,它将默认返回 id,这将使您回到相同的问题。
Try
尝试
TCPWriter* writer = [connection writer];
[writer writeData: data];
or
或者
[(TCPWriter*)[connection writer] writeData: data];
回答by McUsr
As an alternative to the splendid answer above, you can cast the object to the right type to get rid of the warning too, like so:
作为上述出色答案的替代方案,您也可以将对象强制转换为正确的类型以消除警告,如下所示:
[(NSView*)textView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)]; // for horizontal scrolling

