performSelector ARC警告 [英] performSelector ARC warning
问题描述
我在非ARC中使用此代码,无错误或警告:
I have this code in non-ARC that works without errors or warnings:
- (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
}
}
然而,在我转换为项目之后到ARC,我收到此警告:
However, after I convert to project to ARC, I get this warning:
执行选择器可能会导致泄漏,因为它的选择器未知。
"Perform Selector may cause a leak because its selector is unknown."
我很感激有关如何相应地修改我的代码的想法。
I would appreciate ideas on how to revise my code accordingly..
推荐答案
我找到的唯一方法是避免警告就是压制它。您可以在构建设置中禁用它,但我更喜欢使用pragma来禁用它,因为我知道它是假的。
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)
您可以像这样使用宏:
SuppressPerformSelectorLeakWarning([_target performSelector:_action withObject:self]);
这篇关于performSelector ARC警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!