performSelector may cause a leak because its selector is unknown

本文详细介绍了在Objective-C编程中遇到performSelector可能引发内存泄漏警告时的三种解决办法,并提供了相应的代码示例。通过使用runtime替代performSelector调用,忽略特定文件中的警告,或者自定义宏来抑制警告,可以有效避免内存泄漏问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

from: http://stackoverflow.com/questions/7017281/performselector-may-cause-a-leak-because-its-selector-is-unknown

第一种解决办法:

As a workaround until the compiler allows overriding the warning, you can use the runtime

objc_msgSend(_controller, NSSelectorFromString(@"someMethod"));

instead of

[_controller performSelector:NSSelectorFromString(@"someMethod")];

You'll have to

#import <objc/message.h>

第二种:

To ignore the error only in the file with the perform selector, add a #pragma as follows:

#pragma clang diagnostic ignored "-Warc-performSelector-leaks"

第三种:

#define SUPPRESS_PERFORM_SELECTOR_LEAK_WARNING(code)                        \ _Pragma("clang diagnostic push")                                        \ _Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"")     \ code;                                                                   \ _Pragma("clang diagnostic pop")                                         \ SUPPRESS_PERFORM_SELECTOR_LEAK_WARNING( return [_target performSelector:_action withObject:self] );
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值