Gre*_*reg 5 for-loop objective-c nsmutablearray nsarray fast-enumeration
我可以删除我在Objective-C for循环中循环的项目而没有副作用吗?
例如,这样可以吗?
for (id item in items) {
if ( [item customCheck] ) {
[items removeObject:item]; // Is this ok here?
}
Run Code Online (Sandbox Code Playgroud)
McC*_*nus 12
不,如果在快速枚举for循环中改变数组,则会出现错误.制作数组的副本,迭代它,然后从原始数据中删除.
NSArray *itemsCopy = [items copy];
for (id item in itemsCopy) {
if ( [item customCheck] )
[items removeObject:item]; // Is this ok here
}
[itemsCopy release];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3225 次 |
| 最近记录: |