performSelector ARC警告

Dav*_*nte 8 xcode objective-c ios automatic-ref-counting

可能重复:
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
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,在我将项目转换为ARC后,我收到此警告:

"执行选择器可能会导致泄漏,因为它的选择器未知."

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

rob*_*off 43

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

#       pragma clang diagnostic push
#       pragma clang diagnostic ignored "-Warc-performSelector-leaks"
            [_target performSelector:_action withObject:self];
#       pragma clang diagnostic pop
Run Code Online (Sandbox Code Playgroud)

如果您在多个位置收到错误,可以定义宏以便更容易地抑制警告:

#define SuppressPerformSelectorLeakWarning(Stuff) \
    do { \
        _Pragma("clang diagnostic push") \
        _Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
        Stuff; \
        _Pragma("clang diagnostic pop") \
    } while (0)
Run Code Online (Sandbox Code Playgroud)

您可以像这样使用宏:

SuppressPerformSelectorLeakWarning([_target performSelector:_action withObject:self]);
Run Code Online (Sandbox Code Playgroud)