- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有圆弧
测试1:
@interface test01ViewController ()
@property (strong) void(^myBlock)(id obj, NSUInteger idx, BOOL stop);
@end
@implementation test01ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.navigationItem.title = @"test01";
[self setMyBlock:^(id obj, NSUInteger idx, BOOL stop) {
[self doSomethingWithObj:obj];
}];
}
object (self
) 具有对该 block 的显式强引用。并且该 block 具有对 self
的隐式 strong
引用。这是一个循环,现在两个对象都不会被正确释放。
所以 test1 dealloc
没有调用。
测试2:
@interface test03ViewController ()
@property (strong) void(^myBlock)(id obj, NSUInteger idx, BOOL stop);
@end
@implementation test03ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.navigationItem.title = @"test03";
__weak test03ViewController *weakSelf = self;
[self setMyBlock:^(id obj, NSUInteger idx, BOOL stop) {
__strong test03ViewController *strongSelf = weakSelf;
[strongSelf doSomethingWithObj:obj];
}];
}
__weakSelf -> __strongSelf,我觉得跟test1
没有区别,但是test2可以调用dealloc
。
为什么?
最佳答案
看看这个答案:https://stackoverflow.com/a/28475562/543224
Anyway regarding this pattern, capturing a strong reference there doesn't do anything for the case of self getting deallocated before the block runs, that can still happen. It does ensure self doesn't get deallocated while executing the block. This matters if the block does async operations itself giving a window for that to happen.
关于ios - Objective-C ARC block __strong __weak,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34119424/
我看到了两种不同的实现... __weak UIDataType *weakSelf = self; 和 UIDataType __weak *weakSelf = self; 但它们似乎都有效。引擎
我有一个应用程序(仅限 ios5),它在方法内声明了一个在 block 内使用的弱变量,以引用 View Controller 的实例。 QRCodeViewController * __weak
如果对象被释放,Objective-C 弱属性应该指向 nil,但在这种情况下,弱属性似乎保留了对象。考虑这样一个案例: @interface SillyObject : NSObject @prop
在 boost C++ 中,弱指针被实现为共享(引用计数)指针的观察者。 它们是如何在 objective-c 中实现的,为什么这需要运行时支持? (即除了有编译器支持,还需要iOS 5以上才能使用弱
我有以下代码: __weak id weakSelf = self; [geocoder reverseGeocodeLocation:currLocation completionHandl
在ARC中,下面这行代码有意义吗?请确认。 __block __weak MyViewController_iPad *blockSelf = self; 这不会抛出任何错误。不知道为什么。 考虑以下
id obj = [[NSObject alloc] init]; @autoreleasepool{ id __weak weakObj = obj; _objc_autorelea
我从事同一个项目已有一段时间了,随着时间的推移,我对 Objective-C 和 Cocoa 的理解也有了一些进展。回顾我的代码的某些部分,我看到了这一点: __weak ASIFormDataReq
id __weak obj1 = obj0; 等于 id __weak obj1 = obj0; id __autoreleasing tmp = obj0; 适用于 iOS 和 OSX 的 Pro
这是我的代码: extern void _objc_autoreleasePoolPrint(); int main(int argc, const char * argv[]) { NSAr
这两行代码有区别吗? __weak IBOutlet UITextField *usernameField; @property (weak) IBOutlet UITextField *userna
NSString *myString = [NSString stringWithFormat:@"string1"]; __weak NSString *myString1 = myString;
我正在编写一个工具,如果知道类的哪些实例变量被声明为 __weak,将会受益。 此信息必须在运行时存在于某处,但是否有任何方式访问它、记录或以其他方式? (这是一个工具,所以我不太关心它是否会随着更新
如果我向弱对象发送消息会怎样?发送消息是否拥有该对象并将其保存在内存中直到返回? 我在想这个模式: __weak MyObject *weakSelf = self; dispatch_async(d
我想在 block 中使用弱自我,但是在 block 中这个weakSelf变成了nil 刚刚创建,在 block 之前(尝试使用不同的变体) - 看起来一切正常 但后来在 block - 每个变种为
我遇到过这段代码并使用它成功地为按钮上的图像着色,直到它在 iOS 9 发布版本中开始失败: UIImage * __weak image = [[self imageForState:UIContr
从大量关于打破 block 内保留循环的问题中,我的问题如下: 该 block 实际上如何处理其中的 __weak 引用? 我知道这一点(摘自 here ): Blocks will retain a
我有这行代码: __weak NSBlockOperation *weakOperation = operation; 这是触发此编译器错误: __weak attribute cannot be s
Mike Ash has written this introduction to ARC 他介绍了类似的东西: __weak Foo *_weakFoo = [object foo]; 为什么我要对
刚开始使用 ARC。在 ARC 之前,我只是简单地声明我的导出,例如:IBOutlet UIButton *button; 所以我不会保留它或任何东西。对于 ARC,不指定弱或强意味着强。 所以如果我
我是一名优秀的程序员,十分优秀!