Fon*_*nix 2 objective-c ios objective-c-blocks
因此,我尝试在数组中构建块队列,然后在稍后阶段执行队列,队列在使用块中使用的字符串枚举的forloop中构建.
NSArray *array = @[@"test", @"if", @"this", @"works"];
NSMutableArray *queue = [NSMutableArray new];
for(id key in array){
//add the work to the queue
void (^ request)() = ^{
NSLog(@"%@", key);
};
[queue addObject:request];
//request(); //this works fine if i just execute the block here, all the strings are printed
}
for(id block in queue){
void (^ request)() = block;
request(); //this just prints 'works' multiple times instead of all the other strings
}
Run Code Online (Sandbox Code Playgroud)
do块在for循环中不能使用枚举对象(当不在同一个for循环中执行时),或者这看起来像一个bug?
更改
[queue addObject:request];
Run Code Online (Sandbox Code Playgroud)
至
[queue addObject:[request copy]];
Run Code Online (Sandbox Code Playgroud)
更新:块在堆栈中创建.request局部变量也是如此.当你将它添加到NSMutableArray时,它会被保留,但对于块来说还不够!当你离开时,无论如何都会删除阻止{}- 无论是否保留都无关紧要.您应该先将其复制到堆中,然后保留(通过添加到数组中).
| 归档时间: |
|
| 查看次数: |
131 次 |
| 最近记录: |