gpt4 book ai didi

objective-c - NSMutableDictionary 中的 setValue forKey 崩溃

转载 作者:行者123 更新时间:2023-12-01 18:00:53 26 4
gpt4 key购买 nike

我有如下代码。我正在从 Web 服务器检索 JSON。

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{

for (NSMutableDictionary * dataDict in JSON) {


if ([dataDict objectForKey:@"userPicture"])
{

objectForKey:@"userPicture"]);
NSURL *url = [NSURL URLWithString:[dataDict objectForKey:@"userPicture"]];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];


[dataDict setValue:@"userPicture"
forKey:@"userPicture"];


}


[messageList addObject: dataDict];
}

[self processComplete];

}
failure:^(NSURLRequest *request , NSURLResponse *response , NSError *error , id JSON)
{
NSLog(@"Failed: %@", error);

}];

但是它崩溃并出现以下错误:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'



现在,我不明白的是,我正在尝试 setValue:forKey在dataDict中,实例化 NSMutableDictionary ,所以我应该能够设置值。

最佳答案

您已将 dataDict 声明为 NSMutableDictionary ,但这并不意味着您从 JSON 中获得了可变字典。您可以做的是指示您的 JSON 序列化程序获取可变副本(如果它支持此类选项)或手动执行此操作:

for (NSDictionary *dict in JSON) {

NSMutableDictionary * dataDict = [dict mutableCopy];
// Rest of the code here...

}

关于objective-c - NSMutableDictionary 中的 setValue forKey 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10122809/

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