开发者

Mutating method sent to immutable object

开发者 https://www.devze.com 2023-03-24 21:43 出处:网络
I\'m using JSONKit to parse a JSON string returned from my server in my iPhone application. Part of the server response is a couple of Base64 encoded images that I want to decode into actual images an

I'm using JSONKit to parse a JSON string returned from my server in my iPhone application. Part of the server response is a couple of Base64 encoded images that I want to decode into actual images and add to the object that the parser creates. The problem is I can't seem to figure out what kind of class the parser returns, and thus what kind of methods that can be used to interact with the object. I've searched the JSONKit documentation for answers to my question, but haven't found it.

decodedData = [[request responseString] objectFromJSONStrin开发者_如何学Pythong];

int i = 0;
[Base64 initialize];
for (NSString *base64String in [decodedData valueForKey:@"base64String"]) {
    UIImage *image = [UIImage imageWithData:[Base64 decode:base64String]];
    [decodedData setValue:image forKey:@"image"];
    i++;
}

This code is placed in a method that gets called when the request has successfully finished and the response is returned in [request responseString] (the JSON). The decodedData object's class is defined in the header file. No matter what I declare it as (either NSArray, NSMutableArray, NSDictionary, or NSMutableDictionary) I get the same error when the code is run (it compiles just fine), which is:

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

Can anybody tell me what kind of class the object is, and what I should do to add the Base64-decoded image to the object?

I'm pretty new to Objective-C, so bear over with me. Thanks.


You're trying to mutate the immutable JKDictionary (itself a proxy for an immutable NSDictionary object).

The documentation you linked to specifies an instance method on NSString named - (id)mutableObjectFromJSONString;, this will give you a mutable dictionary object that you can play with if you so desire.

0

精彩评论

暂无评论...
验证码 换一张
取 消