简体   繁体   English

自我保留在这个Objective-C块中吗?

[英]Is self retained within this Objective-C block?

When I have a block in Objective-C that looks like this: 当我在Objective-C中有一个块看起来像这样:

self.request = [[ASIHTTPRequest requestWithURL:...

[self.longPollRequest setCompletionBlock:^{
    NSLog(@"%@", self.request.responseString);
}];

will it retain self or explicitly retain self.request ? 它会保留self还是明确地保留self.request

As the Block Programming Topics says: 正如Block Programming Topics所说:

In a reference-counted environment, by default when you reference an Objective-C object within a block, it is retained. 在引用计数环境中,默认情况下,当您引用块中的Objective-C对象时,它将被保留。 This is true even if you simply reference an instance variable of the object. 即使您只是引用对象的实例变量也是如此。 Object variables marked with the __block storage type modifier, however, are not retained. 但是,不保留使用__block存储类型修饰符标记的对象变量。

If you use a block within the implementation of a method, the rules for memory management of object instance variables are more subtle: 如果在方法的实现中使用块,则对象实例变量的内存管理规则更加微妙:

If you access an instance variable by reference, self is retained; 如果通过引用访问实例变量,则保留self;

If you access an instance variable by value, the variable is retained. 如果按值访问实例变量,则保留变量。

you reference self in block, so self is retained. 你在块中引用self ,所以保留self

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

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