简体   繁体   English

NSMutableDictionary中的setValue forKey崩溃

[英]setValue forKey in NSMutableDictionary crashes

I have the code like below. 我有下面的代码。 I am retrieving JSON from a web server. 我正在从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);        

                                     }];

However it crashes with the following error: 但是,它崩溃并显示以下错误:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object' ***由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“-[__ NSCFDictionary setObject:forKey:]:发送给不可变对象的变异方法”

Now, what I don't get is that, I am trying to setValue:forKey in dataDict, which is instantiated NSMutableDictionary , so I should be able to setValue. 现在,我不明白的是,我正在尝试实例化NSMutableDictionary dataDict中的setValue:forKey ,因此我应该能够setValue。

You have declared your dataDict as an NSMutableDictionary but that doesn't mean that you get a mutable dictionary from your JSON. 您已经将dataDict声明为NSMutableDictionary,但这并不意味着您从JSON中获得了可变的字典。 What you could do is either instruct your JSON serializer to fetch you mutable copies (if it supports such option) or manually do this: 您可以做的是要么指示JSON序列化程序获取可变副本(如果它支持此选项),要么手动执行以下操作:

for (NSDictionary *dict in JSON) {

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

}

dataDict is just a pointer, is not instantiated. dataDict只是一个指针,没有实例化。

You are actually dealing with simple NSDictionary , so you cannot modify it. 实际上,您正在处理简单的NSDictionary ,因此无法对其进行修改。 If you need to modify it just call mutableCopy on it. 如果需要修改它,只需对其调用mutableCopy This will make you the owner of the object so, if you are not using ARC, remember to release it when you have done. 这将使您成为对象的所有者,因此,如果您不使用ARC,请记住在完成操作后将其释放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM