xcode performSelector ARC 警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11895287/
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
performSelector ARC warning
提问by David DelMonte
Possible Duplicate:
performSelector may cause a leak because its selector is unknown
I have this code in non-ARC that works without errors or warnings:
我在非 ARC 中有此代码,它可以正常工作而没有错误或警告:
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
{
// Only care about value changed controlEvent
_target = target;
_action = action;
}
- (void)setValue:(float)value
{
if (value > _maximumValue)
{
value = _maximumValue;
} else if (value < _minimumValue){
value = _minimumValue;
}
// Check range
if (value <= _maximumValue & value >= _minimumValue)
{
_value = value;
// Rotate knob to proper angle
rotation = [self calculateAngleForValue:_value];
// Rotate image
thumbImageView.transform = CGAffineTransformMakeRotation(rotation);
}
if (continuous)
{
[_target performSelector:_action withObject:self]; //warning here
}
}
However, after I convert to project to ARC, I get this warning:
但是,在将项目转换为 ARC 后,我收到此警告:
"Perform Selector may cause a leak because its selector is unknown."
“执行选择器可能会导致泄漏,因为它的选择器未知。”
I would appreciate ideas on how to revise my code accordingly..
我将不胜感激有关如何相应地修改我的代码的想法..
回答by rob mayoff
The only way I've found to avoid the warning is to suppress it. You could disable it in your build settings, but I prefer to just use pragmas to disable it where I know it's spurious.
我发现避免警告的唯一方法是抑制它。你可以在你的构建设置中禁用它,但我更喜欢只使用编译指示来禁用它,因为我知道它是虚假的。
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[_target performSelector:_action withObject:self];
# pragma clang diagnostic pop
If you're getting the error in several places, you can define a macro to make it easier to suppress the warning:
如果您在多个地方收到错误消息,您可以定义一个宏来更轻松地抑制警告:
#define SuppressPerformSelectorLeakWarning(Stuff) \
do { \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
Stuff; \
_Pragma("clang diagnostic pop") \
} while (0)
You can use the macro like this:
您可以像这样使用宏:
SuppressPerformSelectorLeakWarning([_target performSelector:_action withObject:self]);