performSelector ARC警告[复制](performSelector ARC warn

2019-06-26 22:25发布

可能重复:
performSelector可能导致泄漏,因为它的选择是未知

我在非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
    }
}

然而,当我转换成投射到ARC,我得到这样的警告:

“执行选择可能会导致泄漏,因为它的选择是未知的。”

我将不胜感激就如何作出相应的修改我的代码的想法..

Answer 1:

我发现,以避免该警告的唯一方法是抑制它。 你可以在你的构建设置禁用它,但我更喜欢只使用编译指示禁用它,我知道这是虚假的。

#       pragma clang diagnostic push
#       pragma clang diagnostic ignored "-Warc-performSelector-leaks"
            [_target performSelector:_action withObject:self];
#       pragma clang diagnostic pop

如果你要在几个地方的错误,你可以定义一个宏,以使其更容易抑制警告:

#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 warning [duplicate]