performSelector ARC警告

可能重复:
performSelector可能会导致泄漏,因为它的select器是未知的

我在非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项目后,我得到这个警告:

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

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

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

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

如果你在几个地方得到了错误,你可以定义一个macros来更容易地抑制这个警告:

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

你可以像这样使用macros:

 SuppressPerformSelectorLeakWarning([_target performSelector:_action withObject:self]);