iOS|iOS 报错(perfrom selector may cause a leak because its selector is unknown)

报错:perfrom selector may cause a leak because its selector is unknown
原因:在ARC模式下,运行时需要知道如何处理你正在调用的方法的返回值。这个返回值可以是任意值,如void,int,char,NSString,id等等。ARC通过头文件的函数定义来得到这些信息。所以平时我们用到的静态选择器就不会出现这个警告。因为在编译期间,这些信息都已经确定。如:

[someController performSelector:@selector(someMethod)]; ... - (void)someMethod{ //bla bla...}

当用[someController performSelector:NSSelectorFromString(@"someMethod")]; 时ARC并不知道该方法的返回值是什么,以及该如何处理?该忽略?还是标记为ns_returns_retained还是ns_returns_autoreleased?
解决方案1:使用函数指针方式
SEL selector = NSSelectorFromString(@"someMethod"); IMP imp = [_controller methodForSelector:selector]; void (*func)(id, SEL) = (void *)imp; func(_controller, selector);

当有额外参数时,如
SEL selector = NSSelectorFromString(@"processRegion:ofView:"); IMP imp = [_controller methodForSelector:selector]; CGRect (*func)(id, SEL, CGRect, UIView *) = (void *)imp; CGRect result = func(_controller, selector, someRect, someView);

解决方案2:使用宏忽略警告
#define SuppressPerformSelectorLeakWarning(Stuff) \ do { \ _Pragma("clang diagnostic push") \ _Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \ Stuff; \ _Pragma("clang diagnostic pop") \ } while (0)

在产生警告也就是performSelector的地方用使用该宏,如
SuppressPerformSelectorLeakWarning( [_target performSelector:_action withObject:self] );

如果需要performSelector返回值的话,
id result; SuppressPerformSelectorLeakWarning( result = [_target performSelector:_action withObject:self] );

解决方案3:使用afterDelay
[self performSelector:aSelector withObject:nil afterDelay:0.0];

【iOS|iOS 报错(perfrom selector may cause a leak because its selector is unknown)】如果在接受范围内,允许在下一个runloop执行,可以这么做。xCode5没问题,但据反映,xcode6的话这个不能消除警告。

    推荐阅读