跳至主要内容

博文

目前显示的是 十二月, 2019的博文

iOS: performSelector may cause a leak because its selector is unknown

参考链接: https://stackoverflow.com/questions/7017281/performselector-may-cause-a-leak-because-its-selector-is-unknown https://stackoverflow.com/questions/13441531/how-to-dynamically-call-a-method-in-objective-c-using-an-nsstring-or-const-char https://stackoverflow.com/questions/11895287/performselector-arc-warning/11895530#11895530 Solution The compiler is warning about this for a reason. It's very rare that this warning should simply be ignored, and it's easy to work around. Here's how: if (! _controller ) { return ; } SEL selector = NSSelectorFromString (@ "someMethod" ); IMP imp = [ _controller methodForSelector : selector ]; void (* func )( id , SEL ) = ( void *) imp ; func ( _controller , selector ); Or more tersely (though hard to read & without the guard): SEL selector = NSSelectorFromString (@ "someMethod" ); (( void (*)( id , SEL ))[ _controller methodForSelector : selector ])( _controller , selector ); Explan...