消除warning:PerformSelector may cause a leak because its selector is unknown

1.原因


This is a warning generated by the compiler because -Wundeclared-selector was used while compiling and automatic reference counting (ARC) is enabled. This can be, in general, safely ignored, as it's obvious that the selector in the variable named "selector" is unknown at compile time, as it will have its value assigned at runtime.


2.消除方法


Anyway, you can supress the warning like this:

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


3.定义宏


If you're getting the error in several places, you can define a macro to make it easier to suppress the warning:

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

You can use the macro like this:

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

If you need the result of the performed message, you can do this:

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


参考资料:

http://www.faqoverflow.com/stackoverflow/7017281.html

http://stackoverflow.com/questions/8773226/performselector-warning


你可能感兴趣的:(iOS,错误处理,OC,Xcode)