使用 ARC 处理面向 4.0 和 5.0 的 iOS 项目.
Working on an iOS project that targets 4.0 and 5.0, using ARC.
遇到与块、ARC 和从块外引用对象相关的问题.这是一些代码:
Running into an issue related to blocks, ARC and referencing an object from outside the block. Here's some code:
__block AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlock:^ {
if ([operation isCancelled]) {
return;
}
... do stuff ...
operation = nil;
}];
在这种情况下,编译器会发出警告,在块中使用操作"将导致保留循环.在 ARC 下,__block 现在保留了变量.
In this case, the compiler gives a warning that using 'operation' in the block is going to lead to a retain cycle. Under ARC, __block now retains the variable.
如果我添加 __unsafe_unretained,编译器会立即释放该对象,所以显然这不起作用.
If I add __unsafe_unretained, the compiler releases the object immediately, so obviously that won't work.
我的目标是 4.0,所以我不能使用 __weak.
I'm targeting 4.0 so I can't use __weak.
我试着做这样的事情:
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
__block __unsafe_unretained AFHTTPRequestOperation *weakOperation = operation;
但是虽然 weakOperation 不是 nil,但它的任何属性都不会在块内填充.
but while weakOperation isn't nil, none of it's properties are populated when inside the block.
鉴于上面列出的项目限制,处理这种情况的最佳方法是什么?
What's the best way to handle this situation given the project constraints listed above?
假设进度保证,保留周期可能正是您想要的.您在块结束时显式中断保留循环,因此它不是永久保留循环:当调用块时,循环被中断.
Assuming progress guarantees, a retain cycle might be exactly what you want. You explicitly break the retain cycle at the end of the block, so it's not a permanent retain cycle: when the block is called, the cycle is broken.
但是,如果您有其他东西保持操作,您可以将引用存储到 __weak 或 __unsafe_unretained 变量中,然后在您的块中使用它.除非您出于某种原因需要在块期间更改变量的绑定,否则无需对变量进行 __block 限定;由于您不再需要中断循环,因此您不需要为弱变量分配任何内容.
If you have something else keeping the operation around, though, you can store a reference into either a __weak or __unsafe_unretained variable and then use that from within your block. There's no need to __block-qualify the variable unless you for some reason need to change the variable's binding during the block; since you don't have a retain cycle to break any more, you shouldn't need to assign anything to the weak variable.
这篇关于ARC、块和保留周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在没有应用程序源代码的情况下使用 Instruments 测Using Instruments to test an iOS app without having source code to the application(在没有应用程序源代码的情况下使用 Instruments 测试
KIF:如何自动运行/压力测试 iOS 应用程序以找出罕KIF: How to auto-run/stress test an iOS app to find the cause of a rare UI bug?(KIF:如何自动运行/压力测试 iOS 应用程序以找出罕见 UI 错
无法更改 Xcode 4.5 中的目标成员身份可见性Can#39;t change target membership visibility in Xcode 4.5(无法更改 Xcode 4.5 中的目标成员身份可见性)
UITableView:在混合单元格表视图静态和动态单元格UITableView: Handle cell selection in a mixed cell table view static and dynamic cells(UITableView:在混合单元格表视图静态和动态单元格中
如何在 iOS 中删除 Safari 中的地址栏?How to remove Address Bar in Safari in iOS?(如何在 iOS 中删除 Safari 中的地址栏?)
升级到 Xcode 4.5 后,iOS 5 SDK 消失了iOS 5 SDK is gone after upgrade to Xcode 4.5(升级到 Xcode 4.5 后,iOS 5 SDK 消失了)