- (void)doIt
{
[_foo bar];
// produces compile time error: "No visible @interface for 'FooClass'
// declares the selector 'bar'"
}
在 Objective-C 的早期,当我调用一个编译器不知道的方法时,我只会得到一个警告。然后,大约一年前,编译器发生了变化,这样的事情现在是错误的。有人知道这是什么时候发生的吗 (__weak
?)
我做了什么来解决这个问题
我知道有几种方法可以以某种方式解决问题。不过,我希望有更简单的方法。
A) 声明一个接口(interface)
@interface FooClass (MichaelHacksIt)
- (void) bar;
@end
// ...
- (void) doIt
{
[_foo bar];
}
B) 使用-performSelector:
- (void)doIt
{
[_foo performSelector:@selector(bar)];
}
C)
因为解决方案 B 有时会产生“Undeclared selector”警告,并且因为我们不想让 Apple 在任何时候都知道我们正在调用哪些方法,所以有第三种解决方案:
- (void)doIt
{
SEL bar_sel = NSSelectorFromString(@"bar");
[_foo performSelector:bar_sel];
}
D)
如果方法调用的参数不仅仅是对象,我们需要使用 NSInvocation
代替。这真的很丑。
我想做什么
是否有一些编译器开关可以禁用错误?在幕后,消息发送始终只是对 objc_msgSend()
的调用,并且链接到方法实现是在运行时完成的。因此,原则上应该可以禁用错误。我想做的是类似于:
- (void)doIt
{
#pragma clang diagnostic push
#pragma clang error disable objc_method_unknown_error
[_foo bar];
#pragma clang diagnostic pop
}
有这样的编译时开关吗?是否有可以在产生这种行为的命令行中传递的编译器标志?
我为什么要问
当您尝试使用未记录的方法时,就会出现这些问题。例如 change the status bar text color in iOS .或至override some undocumented method in a subclass当您拥有源代码,但出于某种原因不想修改它时。
我的解决方案 A 在运行时有什么影响吗?是否有可能以某种方式在二进制文件中看到我声明了一个私有(private)类别 (MichaelHacksIt
),或者它只是让编译器静音? (请注意,此类别没有@implementation
。)
我的主要问题是:是否可以禁用“No visible @interface for 'SomeClass' declares the selector 'fooBar'”?
最佳答案
错误信息已经和Automatic Reference Counting一起介绍了.
有关此特定错误消息的更多信息以及为什么它现在是 ARC 中的 fatal error 可以是 found here .
(这只是原始问题下方评论的摘要)
关于objective-c - 我可以将错误消息 "No visible @interface for ' FooClass' declares the selector 'bar' "转回警告吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23394150/