问题
采用以下方法时会提示【performSelector may cause a leak because its selector is unknown】警告
[_controller performSelector:NSSelectorFromString(@"someMethod")];
解决方案
可采用以下方法消除
if (!_controller) { return; } SEL selector = NSSelectorFromString(@"someMethod"); IMP imp = [_controller methodForSelector:selector]; void (*func)(id, SEL) = (void *)imp; func(_controller, selector);
如果 selector 接收参数,或者有返回值,代码就改为如下:
SEL selector = NSSelectorFromString(@"processRegion:ofView:"); IMP imp = [_controller methodForSelector:selector]; CGRect (*func)(id, SEL, CGRect, UIView *) = (void *)imp; CGRect result = _controller ? func(_controller, selector, someRect, someView) : CGRectZero;