加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – RestKit将MapData:

发布时间:2020-12-16 09:36:07 所属栏目:百科 来源:网络整理
导读:以下代码从我的服务器接收 JSON响应,该响应由一系列元素组成,每个元素都有一个’created_at’和’updated_at’键.对于所有这些元素,我想删除为这两个键设置的字符串中的单个字符(冒号). - (void)objectLoader:(RKObjectLoader*)loader willMapData:(inout id
以下代码从我的服务器接收 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,该消息此类对于密钥created_at不符合密钥值编码.

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

解决方法

你的inout声明对我来说很好看.我建议你用NSLog打印mappableData,看看它实际上是什么样的.

编辑:根据注释中的讨论,在这种情况下,mappableData实际上是JKDictionary对象的集合. JKDictionary在JSONKit.h(RestKit正在使用的JSON解析库)中定义为NSDictionary的子类.因此,它不是一个可变字典,也没有实现[NSMutableDictionary setValue:forKey:].这就是你在运行时获得NSUnknownKeyException的原因.

实现你想要的东西的一种方法可能就是这样(没有经过测试!):

- (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",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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读