开发者

Is self retained within this Objective-C block?

When I have a block in Objective-C that looks like this:

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

[self.longPollReq开发者_如何转开发uest setCompletionBlock:^{
    NSLog(@"%@", self.request.responseString);
}];

will it retain self or explicitly retain self.request?


As the Block Programming Topics says:

In a reference-counted environment, by default when you reference an Objective-C object within a block, it is retained. 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.

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;

If you access an instance variable by value, the variable is retained.

you reference self in block, so self is retained.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜