gpt4 book ai didi

objective-c - 安全使用 performSelector : with a method that returns an allocated object

转载 作者:搜寻专家 更新时间:2023-10-30 20:20:08 25 4
gpt4 key购买 nike

如果我使用的是 ARC,如何使用 performSelector: 调用以下方法而不泄漏?

- (SomeClass *)test {
return [[SomeClass alloc] init];
}

如果我使用类似下面的东西,我会收到一条警告,提示我可能会引入泄漏,因为 ARC 不知道该怎么做,因为选择器是以编程方式生成的。

[object performSelector:selector];

最佳答案

在这种特殊情况下,你没问题。该警告告诉您编译器无法判断 -performSelector: 将返回 +0 还是 +1 对象,并且编译器将继续假定它是 +0 对象。所以是否泄漏取决于调用的实际方法。在这种情况下,您的方法是 -test,它根据命名规则返回一个 +0 对象。所以用-performSelector:调用这个方法不会泄露。

关于objective-c - 安全使用 performSelector : with a method that returns an allocated object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13852132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com