gpt4 book ai didi

ios - UICollectionView 类引用委托(delegate)属性

转载 作者:行者123 更新时间:2023-12-01 18:49:49 26 4
gpt4 key购买 nike

我在 UICollectionView 中发现了一些我无法理解的内容头文件。我发现委托(delegate)有一个分配属性
@property (nonatomic, assign) id <UICollectionViewDelegate> delegate;
这个问题只是为了我的基本理解,因为规则说代表应该有一个弱属性。根据我个人的知识,assign 不会引用委托(delegate)对象的计数,但如果对象被解除分配,它肯定仍然会引用垃圾值。

我如何理解这段代码?

最佳答案

strongweakAutomatic Reference Counting 一起介绍(弧)。 UIKit moved to ARC with iOS 9 ,如果您查看 iOS 9 header (使用 Xcode 7),您将看到该属性现在是 weak .

你是对的:属性为 assign (相当于 unsafe_unretained ),如果委托(delegate)在 Collection View 处于事件状态时被释放, Collection View 的 delegate属性将指向被释放对象曾经所在的位置,并且在引用它时可能会导致崩溃。这通常不是问题,因为委托(delegate)通常是拥有 Collection View 的 View Controller ,因此通常比 View 生命周期更长。但是,这不是保证,这就是为什么您应该设置 assign指向您的代表nil在您的 dealloc .

相关堆栈溢出问题:

  • Objective-C ARC: strong vs retain and weak vs assign
  • Set delegates to nil under ARC?
  • ARC delegate memory management
  • 关于ios - UICollectionView 类引用委托(delegate)属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32068119/

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