执行选择器可能会导致泄漏解决方法 swift 崩溃

Perform selector may cause a leak workaround swift crash

我有一种情况,我想在目标上调用一个方法,而调用执行选择器会给出错误:PerformSelector may cause a leak because its selector is unknown

为了解决这个问题,我使用了 this SO question:

的优秀解决方案
if (self.target) {
    IMP imp = [self.target methodForSelector:self.selector];
    void (*func)(id, SEL, id) = (void *)imp;
    func(self.target, self.selector, argument);
}

现在这段代码是我在 Swift 项目中使用的框架的一部分,它导致了崩溃。

如果我忽略警告并使用 [self.target performSelector:self.selector withObject:self.argument]; 它工作正常。

所以... 我假设这与基本的 Swift 和 Objective-C 消息发送架构有关。请有人告诉我 a) 发生了什么事? b) 如何绕过它。

If I ignore the warnings and use [self.target performSelector:self.selector withObject:self.argument]; It works fine.

那么,我的建议是抑制警告并只调用 performSelector:...

最直接的消息发送方式(假设它接受一个对象参数并且 returns 什么都没有)是:

void (*func)(id, SEL, id) = (void (*)(id, SEL, id))objc_msgSend;
func(self.target, self.selector, self.argument);