gpt4 book ai didi

iphone - NSMutableArray 替换对象

转载 作者:行者123 更新时间:2023-12-03 20:28:02 26 4
gpt4 key购买 nike

我尝试在数组中查找对象,如果成功,我需要将数组中的对象替换为新对象

 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

关于iphone - NSMutableArray 替换对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10457527/

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