首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >performSelector弧警告

performSelector弧警告
EN

Stack Overflow用户
提问于 2012-08-10 03:58:37
回答 1查看 9.5K关注 0票数 8

可能重复: performSelector may cause a leak because its selector is unknown

我在非ARC中有这样的代码,它工作时没有错误或警告:

代码语言:javascript
复制
- (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
    }
}

但是,在我转换到project之后,我会收到以下警告:

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

我想知道如何相应地修改我的代码。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-10 04:33:46

我唯一能避免这种警告的方法就是压制它。您可以在您的构建设置中禁用它,但是我更喜欢在我知道它是假的地方使用语用来禁用它。

代码语言:javascript
复制
#       pragma clang diagnostic push
#       pragma clang diagnostic ignored "-Warc-performSelector-leaks"
            [_target performSelector:_action withObject:self];
#       pragma clang diagnostic pop

如果在几个地方都有错误,您可以定义一个宏,以便更容易地抑制警告:

代码语言:javascript
复制
#define SuppressPerformSelectorLeakWarning(Stuff) \
    do { \
        _Pragma("clang diagnostic push") \
        _Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
        Stuff; \
        _Pragma("clang diagnostic pop") \
    } while (0)

您可以使用这样的宏:

代码语言:javascript
复制
SuppressPerformSelectorLeakWarning([_target performSelector:_action withObject:self]);
票数 43
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11895287

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档