ios之"performSelector may cause a leak because its selector is unknown"警告原因及其解决办法

问题描述

项目中使用到了从字符串创建选择器,编译时发现警告:"performSelector may cause a leak because its selector is unknown"(因为performSelector的选择器未知可能会引起泄漏),为什么在ARC模式下会出现这个警告?

经过搜索后,在Stackoverflow上发现了一个令人满意的答案。见http://stackoverflow.com/questions/7017281/performselector-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];

如果在接受范围内,允许在下一个runloop执行,可以这么做。xCode5没问题,但据反映,xcode6的话这个不能消除警告。

时间: 2024-10-13 16:51:16

ios之"performSelector may cause a leak because its selector is unknown"警告原因及其解决办法的相关文章

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

在ARC项目中使用 performSelector: withObject: 函数出现“performSelector may cause a leak because its selector is unknown”.在stackoverflow找到了一个解决方案,地址:http://stackoverflow.com/questions/7017281/performselector-may-cause-a-leak-because-its-selector-is-unknown. 主要是警

iOS PerformSelector may cause a leak because its selector is unknown

开始的写法: SEL selctor = NSSelectorFromString(_interaction.functionName); [self performSelector:selctor withObject:nil]; 解决办法: 编译器警告这个是由原因的,这种警告很少出现,甚至可以简单的忽略,而且解决也很容易.具体方法如下: SEL selctor = NSSelectorFromString(_interaction.functionName); IMP imp = [self

iOS疯狂详解之warning:performSelector may cause a leak because its selector is unknown

主要是警告信息,在非ARC项目中没有这个警告.如果是在某一处修改只需要加入下列代码: #pragma clang diagnostic push #pragma clang diagnostic ignored "-Warc-performSelector-leaks" [self.ticketTarget performSelector: self.ticketAction withObject: self];//此处是你调用函数的地方 #pragma clang diagnosti

PerformSelector may cause a leak because its selector is unknown

本人使用的方法,需要针对每个warning进行添加 #pragma clang diagnostic push #pragma clang diagnostic ignored "-Warc-performSelector-leaks" id value = [weakSelf performSelector:getSel];//产生warning的代码 #pragma clang diagnostic pop ---------------------------------另一种方

warning:performSelector may cause a leak because its selector

warning:performSelector may cause a leak because its selector     在ARC项目中使用 performSelector: withObject: 函 数出现“performSelector may cause a leak because its selector is unknown”.在stackoverflow找到了一个解决方案,地址:http://stackoverflow.com/questions /7017281/pe

iOS注册远程推送消息证书后提示此证书签发者无效的解决办法

大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 我们在编写关于远程推送消息的App时需要注册一个相关的证书,我们可以到Apple开发网站的Certificates, Identifiers & Profiles页面首先创建一个App ID. 其中进入Push Notifications选项,并且在其子项Development SSL Certificate中选择创建证书(因为该证书是调试用的,所以在Deve

ios弹出软键盘时fixed失效及点击空白处隐藏层的解决办法

一.实现点击按钮弹出层和点击空白处隐藏层,event.stopPropagation()是关键,阻止传导. <script type="text/javascript"> $(function () { $('#btnShow').click(function (event) { event.stopPropagation(); $('#div_apply').toggle(); }); $(document).click(function (event) { $('#di

iOS开发随笔 29:xcode7下模拟器输入文本无法显示系统键盘的解决办法

xcode7下的ios模拟器输入内容无法系统键盘,只能用电脑键盘输入内容,这样可能会对调试带来麻烦. 其实xcode7下的ios模拟器默认只能使用一种,要么是模拟器系统键盘,要么就是是电脑键盘.设置方法如下: 打开ios模拟器菜单栏:Hardware-->Keyboard,会分别看到三个选项:Uses the Same Layout as OS X .Connect Hardware Keyboard和Toggle Software Keyboard.Uses the Same Layout a

Unity3D游戏在iOS上因为trampolines闪退的原因与解决办法

http://7dot9.com/?p=444 http://whydoidoit.com/2012/08/20/unity-serializer-mono-and-trampolines/ 确定具体原因 那么好吧,打一个测试版本再来看,然后再等着崩溃,查看崩溃日志吧,最终看到的崩溃日志中,崩溃线程输出信息如下: Thread 27 Crashed: 0 libsystem_kernel.dylib 0x38e671fc __pthread_kill + 8 1 libsystem_pthrea