gpt4 book ai didi

ios - dispatch_queue_t 定义为强

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:07:18 33 4
gpt4 key购买 nike

我看到了下面一行:

@property (nonatomic, strong) dispatch_queue_t filterMainQueue;

为什么将不是对象的dispatch_queue_t实例声明为strong?

最佳答案

这完全是一个好习惯。我要注意 OS X Mountain Lion 和 iOS 6.0 以来,iOS/OS X 运行时中的所有 GCD 和 XPC 对象都现在 视为 Objective-C 对象ARC 并且它们现在不是原语,因此它们将以与通常的 Objective-C 对象相同的方式进行内存管理。这就是为什么从现在开始您应该将它们声明为 strong

你可以在 object.h 中自己检查:

#define DISPATCH_DECL(name) OS_OBJECT_DECL_SUBCLASS(name, dispatch_object)

这个事实也在docs陈述 :
iOS 6 及更高版本——调度对象(包括队列)是 Objective-C 对象,自动保留和释放。
OS X 10.8 及更高版本——调度对象(包括队列)是 Objective-C 对象,会自动保留和释放。
早期版本——调度对象是自定义对象。您必须手动处理引用计数

还有 object.h 中的注释:

 * By default, dispatch objects are declared as Objective-C types when building
* with an Objective-C compiler. This allows them to participate in ARC, in RR
* management by the Blocks runtime and in leaks checking by the static
* analyzer, and enables them to be added to Cocoa collections.
* See <os/object.h> for details.

关于ios - dispatch_queue_t 定义为强,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24429514/

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