我何时以及为什么要使用ARC将局部变量声明为__weak? [英] When and why would I want to declare a local variable as __weak using ARC?

查看:105
本文介绍了我何时以及为什么要使用ARC将局部变量声明为__weak?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mike Ash写了这篇介绍到ARC ,他介绍了类似的内容:

Mike Ash has written this introduction to ARC where he introduces something like:

__weak Foo *_weakFoo = [object foo];

为什么我要为本地临时变量执行此操作? __weak是一个归零引用,只要引用的对象被释放,它就会自动将_weakFoo指针设置为nil。此外,__ weak仅在iOS> = 5时可用。

Why would I want to do that for a local, temporary variable? __weak is a zeroing reference which will set the _weakFoo pointer automatically to nil as soon as the referenced object gets deallocated. Also, __weak is only available in iOS >= 5.

当我这样做时,我什么时候会遇到麻烦?:

When would I run into trouble when I simply do this?:

Foo *_weakFoo = [object foo];

这总是需要返回一个对象或零。我的猜测是:

This is always expected to return an object or nil. My guess is this:

Foo *_weakFoo = [object foo];
[self doSomethingStupid]; // does something bad so foo gets deallocated
[_weakFoo doIt]; // CRASH! msg sent to deallocated instance 0x123456

仍然让我厌烦的一件事是:它什么时候知道我不再需要一个物体了?我认为当我设置一个指向nil或其他东西的指针时,它会发现该所有者不再需要先前引用的对象,因此可能会消失。但问题是:我把它设置为零。所以它无论如何都是零的!

One thing that still bugs me with ARC is: When does it know that I don't need an object anymore? I'd argue that when I set a pointer to nil or to something else it figures out that the previously referenced object isn't needed by this owner anymore and therefore maybe can go away. But the point is: I set it to nil. So it's nil anyways!

所以当局部变量的__weak有意义时,我必须在其他地方做什么样的疯狂事情以便我真的需要它? / p>

So when would __weak for a local variable make sense, and what kind of crazy thing must I do somewhere else so that I really need that?

推荐答案

如果我必须操纵<$ c,我使用 __弱局部变量$ c> self 在块内部以避免保留周期。考虑这个例子我正在使用GCD和块来执行字符串的网络请求,然后在类声明的标签上设置它,在这种情况下, TurtlesViewController

I use __weak local variables if I have to manipulate self inside of a block to avoid a retain cycle. Consider this example where I'm using GCD and blocks to perform a network request for a string, and then setting it on a label declared by the class, in this case, TurtlesViewController.

__weak TurtlesViewController *weakSelf = self;
dispatch_queue_t networkQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_async(networkQueue, ^{

    // Kick off a network task for some data that is going to populate a label declared on our class
    NSString *returnString = [networkDataSource retrieveTurtleTime];

    // Dispatch back to the main thread to populate the UILabel
    dispatch_async(dispatch_get_main_queue(), ^{

        // Using self.label here creates a retain cycle. Self owns the block and the block has captured self
        self.label.text = returnString;

        // Instead, we use weakSelf for our reference to the label as it will be torn down by ARC at the end of the loop.
        weakSelf.label.text = returnString;
    });
});

这篇关于我何时以及为什么要使用ARC将局部变量声明为__weak?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆