RestKit将MapData:
以下代码从我的服务器接收 JSON 响应,该响应由一个元素数组组成,每个元素都有一个“created_at”和一个“updated_at”键。对于所有这些元素,我想删除为这两个键设置的字符串中的单个字符(冒号)。
- (void)objectLoader:(RKObjectLoader*)loader willMapData:(inout id *)mappableData {
// Convert the ISO 8601 date's colon in the time-zone offset to be easily parsable
// by Objective-C's NSDateFormatter (which works according to RFC 822).
// Simply remove the colon (:) that divides the hours from the minutes:
// 2011-07-13T04:58:56-07:00 --> 2011-07-13T04:58:56-0700 (delete the 22nd char)
NSArray *dateKeys = [NSArray arrayWithObjects:@"created_at", @"updated_at", nil];
for(NSMutableDictionary *dict in [NSArray arrayWithArray:(NSArray*)*mappableData])
for(NSString *dateKey in dateKeys) {
NSString *ISO8601Value = (NSString*)[dict valueForKey:dateKey];
NSMutableString *RFC822Value = [[NSMutableString alloc] initWithString:ISO8601Value];
[RFC822Value deleteCharactersInRange:NSMakeRange(22, 1)];
[dict setValue:RFC822Value forKey:dateKey];
[RFC822Value release];
}
}
但是,行 [dict setValue:RFC822Value forKey:dateKey];
引发 NSUnknownKeyException
,并显示消息 this class is not key valuecoding-sensitive for the key created_at
。
我在这里做错了什么?我的主要问题可能是我对这个 inout 声明不太满意......
The following code receives a JSON response from my server, which consists of an array of elements, each having a 'created_at' and an 'updated_at' key. For all of these elements, I want to remove a single character (a colon) in the strings that are set for these two keys.
- (void)objectLoader:(RKObjectLoader*)loader willMapData:(inout id *)mappableData {
// Convert the ISO 8601 date's colon in the time-zone offset to be easily parsable
// by Objective-C's NSDateFormatter (which works according to RFC 822).
// Simply remove the colon (:) that divides the hours from the minutes:
// 2011-07-13T04:58:56-07:00 --> 2011-07-13T04:58:56-0700 (delete the 22nd char)
NSArray *dateKeys = [NSArray arrayWithObjects:@"created_at", @"updated_at", nil];
for(NSMutableDictionary *dict in [NSArray arrayWithArray:(NSArray*)*mappableData])
for(NSString *dateKey in dateKeys) {
NSString *ISO8601Value = (NSString*)[dict valueForKey:dateKey];
NSMutableString *RFC822Value = [[NSMutableString alloc] initWithString:ISO8601Value];
[RFC822Value deleteCharactersInRange:NSMakeRange(22, 1)];
[dict setValue:RFC822Value forKey:dateKey];
[RFC822Value release];
}
}
However, the line [dict setValue:RFC822Value forKey:dateKey];
raises an NSUnknownKeyException
with the message this class is not key value coding-compliant for the key created_at
.
What am I doing wrong here? My main issue is maybe that I don't really feel comfortable with this inout declaration...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的 inout 声明对我来说看起来没问题。我建议你用 NSLog 打印 mappableData 来看看它实际上是什么样子。
编辑:根据评论中的讨论,本例中的
mappableData
实际上是JKDictionary
对象的集合。JKDictionary
在JSONKit.h
(RestKit 使用的 JSON 解析库)中定义为NSDictionary
的子类。因此,它不是可变字典,并且不实现[NSMutableDictionary setValue:forKey:]
。这就是为什么你在运行时收到 NSUnknownKeyException 的原因。实现你想要的一种方法可能是这样的(未经测试!):
Your inout declaration looks OK to me. I suggest you to print mappableData with NSLog to see what does it actually look like.
Edit: Based on the discussion in the comments,
mappableData
in this case is actually a collection ofJKDictionary
objects.JKDictionary
is defined inJSONKit.h
(a JSON parsing library that RestKit is using) as a subclass ofNSDictionary
. Therefore, it's not a mutable dictionary and does not implement[NSMutableDictionary setValue:forKey:]
. That's why you're getting NSUnknownKeyException during runtime.One way to achieve what you want could be something like that (not tested!):