问题描述
我想在NSObject实例上使用选择器,而无需实现协议.例如,如果要调用的NSObject实例支持它,则有一个category方法应该设置一个error属性.这是代码,并且代码按预期工作:
I want to use a selector on an NSObject instance without the need for an implemented protocol. For example, there's a category method that should set an error property if the NSObject instance it's called on supports it. This is the code, and the code works as intended:
if ([self respondsToSelector:@selector(setError:)])
{
[self performSelector:@selector(setError:) withObject:[NSError errorWithDomain:@"SomeDomain" code:1 userInfo:nil]];
}
但是,编译器在setError:签名周围看不到任何方法,因此对于包含@selector(setError:)
代码段的每一行,它都会向我发出警告:
However, the compiler doesn't see any method around with the setError: signature, so it gives me a warning, for each line that contains the @selector(setError:)
snippet:
Undeclared selector 'setError:'
我不想声明一个协议来摆脱此警告,因为我不希望所有可能使用该类的类来实现任何特殊的东西.按照约定,我希望它们具有setError:
方法或属性.
I don't want to have to declare a protocol to get rid of this warning, because I don't want all classes that may use this to implement anything special. Just by convention I want them to have a setError:
method or property.
这可行吗?怎么样?
干杯,
EP
Cheers,
EP
推荐答案
另一种选择是通过以下方式禁用警告:
Another option would be to disable the warning with:
#pragma GCC diagnostic ignored "-Wundeclared-selector"
您可以将此行放置在发生警告的.m文件中.
You can place this line in the .m file where the warning occurs.
更新:
它也可以像这样与LLVM一起使用:
It works also with LLVM like this:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
... your code here ...
#pragma clang diagnostic pop
这篇关于如何摆脱“未声明选择器"警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!