2012-08-10 68 views
7

可能重複:
performSelector may cause a leak because its selector is unknownperformSelector ARC警告

我在非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項目,我得到這個警告:

「執行選擇器可能會導致泄漏,因爲其選擇器未知。」

我將不勝感激就如何修改我的代碼,相應的想法..

回答

40

我發現,以避免該警告的唯一方法是抑制它。你可以在你的構建設置中禁用它,但我更喜歡只使用編譯指示來禁用它,因爲我知道它是虛假的。

#  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]); 
+0

感謝羅布。你知道這是否有雷達?大衛 – 2012-08-10 04:36:55

+0

可能是相關的:http://stackoverflow.com/questions/11875900/crash-in-objc-retain-in-method-performed-with-performselector – Jessedc 2012-08-10 04:49:22

+0

@DavidDelMonte我還沒有提交它的雷達。我不知道其他人可能提交了哪些雷達。 – 2012-08-11 18:00:07