gpt4 book ai didi

ios - -[__NSCFDictionary setObject :forKey:]: mutating method sent to immutable object

转载 作者:可可西里 更新时间:2023-11-01 05:01:45 28 4
gpt4 key购买 nike

运行以下代码时,[dict setValue:@"null"forKey:@"name"]; 一直崩溃。我在这里搜索,发现其他帖子是由不使用 NSMutableDictionary 的人引起的。但是我正在使用它。

如果 namenull,为什么会在这一行崩溃?

NSMutableArray *tempCustomers = [[NSMutableArray alloc] init];
for (NSMutableDictionary *dict in [[json objectForKey:@"data"] mutableCopy]) {
if ([dict objectForKey:@"name"] == [NSNull null]) {
[dict setValue:@"null" forKey:@"name"];
}
[tempCustomers addObject:dict];
}

最佳答案

我最终使用了这个。我猜这就是 deepMutableCopy 是什么?

NSMutableArray *tempCustomers = [[NSMutableArray alloc] init];
for (NSMutableDictionary *dict in [[json objectForKey:@"data"] mutableCopy]) {
if ([dict objectForKey:@"name"] == [NSNull null]) {
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
tempDict = [dict mutableCopy];

[tempDict setValue:@"null" forKey:@"name"];
[tempCustomers addObject:tempDict];
} else {
[tempCustomers addObject:dict];
}
}

关于ios - -[__NSCFDictionary setObject :forKey:]: mutating method sent to immutable object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12677373/

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