iphone - self 保留在这个 Objective-C block 中吗?

标签 iphone objective-c objective-c-blocks

当我在 Objective-C 中有一个如下所示的 block 时:

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

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

它会保留 self 还是显式保留 self.request

最佳答案

作为Block Programming Topics说:

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/

相关文章:

iphone - 如何将视频传输到 iPhone/黑莓?

iphone - Collection View 中的单元格不可见

objective-c - 使用未声明的标识符 : Area and Perimeter of a Rectangle

ios - block 内不允许解除对 __weak 指针的引用

ios - 如何编写 Objective-C 完成 block

iphone - 将秒转换为格式化时间,例如 12 :59?

iphone - Xcode - 在自定义配置中排除文件 - 更好的方法?

iphone - 有没有办法模拟多个 iOS 设备运行 Bonjour 网络应用程序?

ios - 如何对 c 结构体数组使用 NSCoding? (MK 折线)

cocoa-touch - 重复 UIAnimation block ,以及再次停止它的方法