我尝试在数组中查找对象,如果成功,则需要将数组中的对象替换为新对象
for (id existingSig in allSignature)
if ([[existingSig objectForKey:@"SignatureName"] isEqualToString:[item objectForKey:@"name"]])
{
[allSignature removeObject:existingSig];
[allSignature addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"1", @"SignatureIsRich", [item objectForKey:@"name"], @"SignatureName", generatedID, @"SignatureUniqueId", nil]];
}
我有错误'NSCFArray:0x100551f10>在枚举时发生了变异'
最佳答案
正如其他人所说,枚举MutableArray时不能对其进行突变。您可以通过在循环后有两个要删除的内容和要添加的内容的数组来处理它。
NSMutableArray *remove = [NSMutableArray array];
NSMutableArray *add = [NSMutableArray array];
for (id existingSig in allSignature){
if ([[existingSig objectForKey:@"SignatureName"] isEqualToString:[item objectForKey:@"name"]])
{
// Add to the array of objects to be removed
[remove addObject:existingSig];
// Add to the array of objects to be added
[add addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"1", @"SignatureIsRich", [item objectForKey:@"name"], @"SignatureName", generatedID, @"SignatureUniqueId", nil]];
}
}
[allSignature removeObjectsInArray:remove]; // Remove objects
[allSignature addObjectsFromArray:add]; // Add new objects