简体   繁体   English

弱引用难题

[英]Weak references conundrum

Suppose I want to create a weak reference so it will not be retained by a block.假设我想创建一个弱引用,这样它就不会被块保留。 So I have a property:所以我有一个属性:

@property (nonatomic, strong) MyClass *myObject;

and I do this:我这样做:

__weak typeof(self.myObject) myObjectWeak = self.myObject;

void (^doSomething)() = ^void() {
   [myObjectWeak doSomething];    
};

but what about this:但是这个呢:

__weak typeof(self) selfWeak = self;

void (^doSomething)() = ^void() {
   [selfWeak.myObject doSomething];    
};

Will this produce the same effect?这会产生同样的效果吗? I mean having a weak reference to myObject inside the block?我的意思是在块内对 myObject 有一个弱引用? I suspect it is not, but I just need to confirm.我怀疑它不是,但我只需要确认。

Yes, using selfWeak in that last example is sufficient to break the strong reference cycle.是的,在最后一个例子中使用selfWeak足以打破强引用循环。 Don't worry about the fact that you reference its properties in there.不要担心你在那里引用了它的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM