发送到不可变对象的变异方法

发布于 2024-11-27 19:26:48 字数 1177 浏览 3 评论 0原文

我正在使用 JSONKit 解析从 iPhone 应用程序中的服务器返回的 JSON 字符串。服务器响应的一部分是几个 Base64 编码图像,我想将它们解码为实际图像并添加到解析器创建的对象中。问题是我似乎无法弄清楚解析器返回什么样的类,以及可以使用什么样的方法与对象交互。我已经在 JSONKit 文档 中搜索了我的问题的答案,但没有找到答案没找到。

decodedData = [[request responseString] objectFromJSONString];

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++;
}

此代码放置在一个方法中,当请求成功完成并且响应以 [request responseString] (JSON)返回时,该方法将被调用。 decodedData 对象的类在头文件中定义。无论我将其声明为什么(NSArrayNSMutableArrayNSDictionaryNSMutableDictionary),我都会得到相同的结果代码运行时出现错误(它编译得很好),即:

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

任何人都可以告诉我该对象是什么类型的类,以及我应该如何将 Base64 解码的图像添加到对象中?

我对 Objective-C 还很陌生,所以请耐心等待。谢谢。

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] objectFromJSONString];

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

っ左 2024-12-04 19:26:48

您正在尝试改变不可变的 JKDictionary (本身是不可变的 NSDictionary 对象的代理)。

您链接到的文档指定了 NSString 上名为 - (id)mutableObjectFromJSONString; 的实例方法,这将为您提供一个可变字典对象,如果您愿意,您可以使用它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文