解决performSelector may cause a leak because its selector is unknown警告

2021-12-06 19:37:05 浏览数 (3)

问题

采用以下方法时会提示【performSelector may cause a leak because its selector is unknown】警告

代码语言:javascript复制
[_controller performSelector:NSSelectorFromString(@"someMethod")];

解决方案

可采用以下方法消除

代码语言:javascript复制
if (!_controller) { return; }
SEL selector = NSSelectorFromString(@"someMethod");
IMP imp = [_controller methodForSelector:selector];
void (*func)(id, SEL) = (void *)imp;
func(_controller, selector);

如果 selector 接收参数,或者有返回值,代码就改为如下:

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

原文地址:https://stackoverflow.com/questions/7017281/performselector-may-cause-a-leak-because-its-selector-is-unknown

0 人点赞