8

可能重复:
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 后,我收到以下警告:

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

我将不胜感激有关如何相应地修改我的代码的想法..

4

1 回答 1

43

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

#       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]);
于 2012-08-10T04:33:46.597 回答