Objective c RestKit willMapData:

Objective c RestKit willMapData:,objective-c,restkit,Objective C,Restkit,下面的代码从我的服务器接收一个JSON响应,它由一个元素数组组成,每个元素都有一个“created_at”和一个“updated_at”键。对于所有这些元素,我希望删除为这两个键设置的字符串中的单个字符(冒号) - (void)objectLoader:(RKObjectLoader*)loader willMapData:(inout id *)mappableData { // Convert the ISO 8601 date's colon in the time-zone of

下面的代码从我的服务器接收一个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
,并显示消息
此类不符合在
处创建的密钥的键值编码


我做错了什么?我的主要问题可能是我对这个inout声明感到不舒服…

我觉得你的inout声明很好。我建议您使用NSLog打印mappableData,以查看它的实际外观

编辑:根据评论中的讨论,在本例中,
mappableData
实际上是
jkdograry
对象的集合
jkdograry
JSONKit.h
(RestKit正在使用的JSON解析库)中定义为
NSDictionary
的子类。因此,它不是一个可变字典,并且不实现
[NSMutableDictionary setValue:forKey://code>。这就是为什么在运行时会出现NSUnknownKeyException

实现您想要的目标的一种方法可以是类似的(未经测试!):


好的,输出看起来和我期望的一样:(摘录)
({“\u id”=4e1c19b5273d60001000007;“created\u at”=“2011-07-12T02:55:29-07:00”;“updated\u at”=“2011-07-12T02:55:29-07:00”;“user\u id”=4d1180941f6df605180000002;})
真奇怪。当你把冒犯性的陈述记录下来时会发生什么?它打印什么?您确定它是可变字典吗?看起来与上面的输出相同,只是没有普通括号。这对我来说很有意义,因为它不再是数组,而是一个dict条目。另外,在我看来,RestKit规范中有一个完全符合我的要求,即使没有任何转换:我的另一个想法是将“for(NSMutableDictionary*dict-in”替换为“for(id-dict-in)”,并在有问题的语句之前记录[dict-Class]。JKDictionary就是它告诉我的。
- (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];
    NSMutableArray *reformattedData = [NSMutableArray arrayWithCapacity:[*mappableData count]];

    for(id dict in [NSArray arrayWithArray:(NSArray*)*mappableData]) {
        NSMutableDictionary* newDict = [dict mutableCopy];
        for(NSString *dateKey in dateKeys) {
            NSMutableString *RFC822Value = [[newDict valueForKey:dateKey] mutableCopy];
            [RFC822Value deleteCharactersInRange:NSMakeRange(22, 1)];
            [newDict setValue:RFC822Value forKey:dateKey];
        }
        [reformattedData addObject:newDict];
    }
    *mappableData = reformattedData;
}