gpt4 book ai didi

objective-c - 如何释放通过 objc_setAssociatedObject 关联的对象?

转载 作者:太空狗 更新时间:2023-10-30 03:27:55 26 4
gpt4 key购买 nike

如果我使用一个类别,该类别使用 objc_setAssociatedObject 向对象添加伪属性,我如何确保它们被正确释放?我是否必须在 dealloc 中手动释放它们?

最佳答案

您分配的行为决定了如何为该类型管理内存。如果您选择保留或复制,则当您添加关联引用的类的实例被释放时,该对象将被释放。这使得保留和复制行为操作成为 Objective-C 类的首选。只要在完成对象后将引用设置为 nil,Assign 也可用于避免循环保留。

Constants

OBJC_ASSOCIATION_ASSIGN Specifies a weak reference to the associated object.

OBJC_ASSOCIATION_RETAIN_NONATOMIC Specifies a strong reference to the associated object, and that the association is not made atomically.

OBJC_ASSOCIATION_COPY_NONATOMIC Specifies that the associated object is copied, and that the association is not made atomically.

OBJC_ASSOCIATION_RETAIN Specifies a strong reference to the associated object, and that the association is made atomically.

OBJC_ASSOCIATION_COPY Specifies that the associated object is copied, and that the association is made atomically.

关于objective-c - 如何释放通过 objc_setAssociatedObject 关联的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9279502/

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