ios - RetainCycle 与 Globals

标签 ios objective-c objective-c-blocks retain-cycle

假设在 iOS 中我们有:

在 W2AppDelegate.m 中:

GlobalViewController *globalVc;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...

}

在 SomeOtherViewController.m 中:

- (void)viewDidLoad {
    [super viewDidLoad];

    [globalVc doSomething^{
       [globalVc.someVariable doSomethingElse]; // Is there a retain cycle here?
    }];
}

这里是否有一个循环引用,因为我们在 block 中有一个对 globalVc 的强引用。

globalVc -> block -> globalVc

最佳答案

Is there a retain cycle here since we have a strong reference to globalVc inside the block.

没有。因为 block 只捕获局部变量。

[globalVc doSomething^{
   [globalVc.someVariable doSomethingElse]; // Is there a retain cycle here?
}];

globalVc 没有被 block 捕获,因为 globalVc 是全局变量。这里没有局部变量,所以该 block 不捕获任何对象,因此该 block 根本不保留任何对象。

关于ios - RetainCycle 与 Globals,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31465676/

相关文章:

ios - CorePlot - 调用 barTouchUpAtRecordIndex 后更改 x 轴标签 :

objective-c - UIButton子类化时,UIControlState获取值的名称是什么?

ios - 使用 isgl3D 未正确进行 3D 对象 (.pod) 中的纹理映射

ios - 在不同的线程中获取和使用 NSManagedObject

objective-c - 在 Swift 协议(protocol)中使用 ObjC block

ios - 如何从字典的 NSArray 创建 indexPath

ios - 如何在用户行走时跟踪 GPS 坐标,来自 ios 平台的 xamarin.forms

ios - PhoneGap 应用程序在显示内存警告后崩溃

ios - 如何在 Google Drive API (Swift 5) 中禁用文件夹递归

ios - 了解objc中 block 存储管理的一种极端情况