gpt4 book ai didi

objective-c - 由于未捕获的异常 'NSGenericException' 而终止应用程序,原因 : '*** Collection <__NSArrayM: 0x138cb4d0> was mutated while being enumerated

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:03:44 25 4
gpt4 key购买 nike

我正在尝试从数组中删除重复的对象。

 NSMutableArray* filterResults = [[NSMutableArray alloc] init];
BOOL copy;

// remove duplicate
if (![arrSelectedVehicle count] == 0)
{
for (Vehicles *a1 in arrSelectedVehicle) {
copy = YES;
for (Vehicles *a2 in filterResults) {
if ([a1.Vehicle_id isEqualToString:a2.Vehicle_id]) {
copy = NO;
[arrSelectedVehicle removeObjectIdenticalTo:a2];
break;
}
}
if (copy) {
[filterResults addObject:a1];
}
}
}

我正在添加两个已经在数组中的对象

最佳答案

枚举时不能修改数组。您可以执行以下操作:

 NSMutableArray* filterResults = [[NSMutableArray alloc] init];
BOOL copy;

// remove duplicate
if (![arrSelectedVehicle count] == 0)
{
NSArray* arraycopy = [arrSelectedVehicle copy];
for (Vehicles *a1 in arraycopy) {
copy = YES;
for (Vehicles *a2 in filterResults) {
if ([a1.Vehicle_id isEqualToString:a2.Vehicle_id]) {
copy = NO;
[arrSelectedVehicle removeObjectIdenticalTo:a2];
break;
}
}
if (copy) {
[filterResults addObject:a1];
}
}
[arraycopy release];
}

关于objective-c - 由于未捕获的异常 'NSGenericException' 而终止应用程序,原因 : '*** Collection <__NSArrayM: 0x138cb4d0> was mutated while being enumerated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14152471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com