Objective c “我该如何解决?”;实例方法[…]未找到“;错误?
我正在开发一个Cocoa应用程序。据我所知,在尝试调用选择器中的方法时,我所做的一切都是正确的。例如(摘要): 标题.hObjective c “我该如何解决?”;实例方法[…]未找到“;错误?,objective-c,macos,cocoa,selector,Objective C,Macos,Cocoa,Selector,我正在开发一个Cocoa应用程序。据我所知,在尝试调用选择器中的方法时,我所做的一切都是正确的。例如(摘要): 标题.h @interface Foo @property (nonatomic) BOOL variable; - (void)bar; @end 标题.m #import "Header.h" @implementation Foo @synthesize variable; - (void)bar { NSLog("Test"); } @end #imp
@interface Foo
@property (nonatomic) BOOL variable;
- (void)bar;
@end
标题.m
#import "Header.h"
@implementation Foo
@synthesize variable;
- (void)bar
{
NSLog("Test");
}
@end
#import "Header.h"
@implementation OtherFoo
- (IBAction)otherBar:(id)sender
{
Foo *foo = [sender representedObject];
[foo bar]; // Generates warning
BOOL variable = [foo variable]; // No warning
}
@end
OtherHeader.m
#import "Header.h"
@implementation Foo
@synthesize variable;
- (void)bar
{
NSLog("Test");
}
@end
#import "Header.h"
@implementation OtherFoo
- (IBAction)otherBar:(id)sender
{
Foo *foo = [sender representedObject];
[foo bar]; // Generates warning
BOOL variable = [foo variable]; // No warning
}
@end
执行此操作时,表达式[foo-bar]
会收到警告“实例方法”-bar”未找到(返回类型默认为“id”)
我意识到类似的问题已经被问了很多次了,但我已经浏览了这些(以及cocoa和selectors的文档)这一错误最常见的原因似乎是遗漏了bar
的实现或对Header.h的引用,但正如您所看到的,我两者都有
EDIT:从NSMenuItem
的实例调用选择器-这就是我调用representedObject
的原因
编辑2:由于某种原因,其他实例方法和属性不会生成警告。只是特定的实例方法。太奇怪了。哇。我是个白痴。这个问题是由于我没有构建它造成的。由于
OtherHeader.h
在一个单独的项目中,XCode自动完成无法在库文件中看到它,直到l该项目已完成
因此,从这一点和我之前的互联网研究中,我将总结出我所知道的导致这一错误的原因:
我希望这能帮助像我这样花了几个小时困惑地盯着这个错误的人。如果你这样做会发生什么:
Foo*Foo=(Foo*)[sender representedObject]
?不幸的是,使用C样式转换时也会发生同样的情况。不过,这是个好主意。为什么要调用representedObject
?此方法的文档在哪里?抱歉,我想我应该提到,如果将方法更改为-(iAction)otherBar:(NSMenuItem*),则会从一个实例调用选择器发件人
?