gpt4 book ai didi

ios - 在for循环中将NSDictionary转换为NSMutableDictionary

转载 作者:行者123 更新时间:2023-12-01 18:14:33 24 4
gpt4 key购买 nike

我有以下代码:

for (NSMutableDictionary *aDict in array)
{
// do stuff
[aDict setObject:myTitle forKey:@"title"];
}

我的问题是,如果 array中充满了 NSDictionary对象,编写的for循环代码会自动将其转换为 NSMutableDictionary对象吗?

还是我需要在这里做一些更具体的事情以确保在循环中不会在 unrecognized selector sent to instance上收到 setObject:forKey:错误?

最佳答案

目前,这将给您您提到的错误。尽管使用可变字典设置了循环,但基础对象仍然是不可变的。您需要从中创建一个新词典。尝试这个

NSMutableArray *newArray = [NSMutableArray array];
for (NSDictionary *aDict in array)
{
NSMutableDictionary *mutable = [aDict mutableCopy];
// do stuff
[mutable setObject:myTitle forKey:@"title"];
[newArray addObject:mutable];
}

关于ios - 在for循环中将NSDictionary转换为NSMutableDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23659614/

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