执行选择弧ARC警告

可能重复:
由于其选择器未知,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项目后,出现以下警告:

“执行选择器可能会导致泄漏,因为其选择器未知。”

我会很感激如何相应地修改我的代码。


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

#       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]);
链接地址: http://www.djcxy.com/p/19947.html

上一篇: performSelector ARC warning

下一篇: Action design pattern became bad practice under ARC?