【发布时间】:2013-10-16 00:37:50
【问题描述】:
关于在 Objective-C 中使用 ARC 时何时需要复制块,我得到了相互矛盾的信息。建议从“总是”到“从不”不等,所以我真的不知道该怎么做。
我碰巧遇到一个不知道怎么解释的案例:
-(RemoverBlock)whenSettledDo:(SettledHandlerBlock)settledHandler {
// without this local assignment of the argument, one of the tests fails. Why?
SettledHandler handlerFixed = settledHandler;
[removableSettledHandlers addObject:handlerFixed];
return ^{
[removableSettledHandlers removeObject:handlerFixed];
};
}
使用这样的块内联调用:
-(void) whatever {
[self whenSettledDo:^(...){
...
}];
}
(The actual code this snipper was adapted from is here.)
在这里将参数复制到局部变量会发生什么变化?没有本地的版本是否制作了两个不同的副本,一个用于 addObject,一个用于 removeObject,因此删除的副本与添加的副本不匹配?
ARC 为何或何时不能正确处理块?它保证什么,我的责任是什么?所有这些都以非模糊的方式记录在哪里?
【问题讨论】:
标签: objective-c automatic-ref-counting objective-c-blocks