当我在 Objective-C 中有一个如下所示的 block 时:
self.request = [[ASIHTTPRequest requestWithURL:...
[self.longPollRequest setCompletionBlock:^{
NSLog(@"%@", self.request.responseString);
}];
它会保留 self
还是显式保留 self.request
?
最佳答案
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.
你在 block 中引用了self
,所以self
被保留了。
关于iphone - self 保留在这个 Objective-C block 中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6433790/