假设在 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/