XCode 编译器警告:'foo' 可能不会响应 -bar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1150450/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:41:31  来源:igfitidea点击:

XCode compiler warning: 'foo' may not respond to -bar

iphoneobjective-cxcode

提问by sinissaar

I'm trying to get my head around Objective-C for the iPhone. My app is compiling and running just fine so far, but I'm getting a compiler warning that I cannot get rid of.

我正在努力了解 iPhone 的 Objective-C。到目前为止,我的应用程序正在编译和运行得很好,但是我收到了一个编译器警告,我无法摆脱。

Header for one class: (snipped)

一类标题:(剪断)

@interface PersonDetailViewController : UIViewController {
    NSDictionary *person;
}
@property (retain) NSDictionary *person;
@end

Implementation for this class: (also snipped)

这个类的实现:(也剪掉了)

#import "PersonDetailViewController.h"
@implementation PersonDetailViewController
@synthesize person;
@end

I'm creating an instance of PersonDetailViewController in PersonListViewController and calling:

我在 PersonListViewController 中创建 PersonDetailViewController 的一个实例并调用:

#import "PersonListViewController.h"
#import "Person.h"
#import "PersonDetailViewController.h"

@implementation PersonListViewController
- (IBAction)myMethod:(id)sender {
    NSDictionary *person = [[Person alloc] initFromTestArray:[sender tag]];
    [personDetailViewController setPerson:person];
    [[personDetailViewController person] describe];
}
@end

And I'm then informed that:

然后我被告知:

warning: 'UIViewController' may not respond to '-setPerson' (Messages without a matching method signature will be assumed to return 'id' and accept '...' as arguments.)
warning: 'UIViewController' may not respond to '-person'

it does actually respond just fine, but I cannot figure out how to organise my headers so that the compiler would know what will respond...

它实际上响应得很好,但我不知道如何组织我的标题,以便编译器知道会响应什么......

I'm all out of Google... hope I've given enough info and somebody can help.

我完全不在谷歌......希望我已经提供了足够的信息并且有人可以提供帮助。

Thanks a heap!

谢谢一堆!

回答by zoul

Apparently you have personDetailViewControllerdeclared as UIViewController? You can cast the controller explicitly:

显然你已经personDetailViewController声明为UIViewController?您可以显式转换控制器:

[(PersonDetailViewController*)personDetailViewController setPerson:person];

But boy this is ugly. It would be better to simply declare the personDetailViewControlleras PersonDetailViewControllerin the PersonListViewControllerheader. I hope I got it right, I got a bit dazed by all the long names :)

但是男孩这很丑陋。最好在标题中简单地声明personDetailViewControlleras 。我希望我做对了,所有的长名字都让我有点头晕:)PersonDetailViewControllerPersonListViewController

回答by micmoo

I'm a bit confused... Why would you do this:

我有点困惑......你为什么要这样做:

NSDictionary *person = [[Person alloc] initFromTestArray:[sender tag]];

Your declaring person to be an NSDictionary, but are init'ing a Person class... Shouldn't it be

你声明的人是一个 NSDictionary,但正在初始化一个 Person 类......不应该是

Person *person = [[Person alloc] initFromTestArray:[sender tag]];

editI didn't notice the @property & synthesize, so you were correct there, my bad... the accept answer is what you were looking for!

编辑我没有注意到@property & 合成,所以你在那里是对的,我的错......接受答案就是你要找的!