gpt4 book ai didi

objective-c - 数组枚举期间应用程序崩溃

转载 作者:行者123 更新时间:2023-11-28 17:59:50 25 4
gpt4 key购买 nike

这段代码有什么问题?我明白了

 Collection <NSCFArray: 0x101e1b6d0> was mutated while being enumerated

是NSMutableArray,不是NSArray

NSMutableArray *set = [[NSMutableArray alloc]initWithObjects:@"first", @"second", @"third", @"third", nil];

[set enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{
if([obj isEqualToString:@"third"])
{
[set removeObjectAtIndex:idx];
}
}];

[pool drain];

最佳答案

问题是您在枚举期间更改了数组。这是不行的。

请阅读选定的答案

Best way to remove from NSMutableArray while iterating?

所有你需要的都在那里。

关于objective-c - 数组枚举期间应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9465709/

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