作为委托引用的对象范围 [英] Scope of object referenced as a delegate

查看:164
本文介绍了作为委托引用的对象范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要确认。

我有这样的代码

@implementation A
    (void) read
    {
        NSURLConnection *conn = 
           [[NSURLConnection alloc] initWithRequest:request delegate:self];

        ...    
    }
@end

@implementation B
   (void) someFn() 
   {
      A anObject = [A alloc] init]
      [anObject read]
   }

@end

在连接完成之前,将 anObject 成为一个有效对象,因为它作为代理传递给NSURLConnection?

Would anObject be a valid object until connection finishes since it is passed as a delegate to NSURLConnection??

推荐答案

如果您使用ARC,那么我想这将会。

If you use ARC, then I suppose it will.

A 只有在连接结束时才可以释放,因为持有引用的连接是非常连接的。

A will only be releasable when the connection will be ended, since the one which is holding a reference is that very connection.

然后,您在 B 中执行另一个引用。

Then, you hold another reference to A with your implementation in B.

因此,如果 anObject someFn()中的某个时间未使用,并且内部引用需要它保持活着,它将有效,直到它没有内部参考。

So, if anObject is unused at sometime in someFn() AND has inside reference needing it to be kept alive, it'll be valid until it has no inside reference.

这篇关于作为委托引用的对象范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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