3

次のコードは、サーバーからJSON応答を受け取ります。これは、要素の配列で構成され、それぞれに「created_at」キーと「updated_at」キーがあります。これらすべての要素について、これら2つのキーに設定されている文字列の1文字(コロン)を削除したいと思います。

- (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];せます。NSUnknownKeyExceptionthis class is not key value coding-compliant for the key created_at

私はここで何が間違っているのですか?私の主な問題は、おそらく私がこのinout宣言に本当に満足していないということです...

4

1 に答える 1

7

あなたのinout宣言は私には大丈夫に見えます。NSLogを使用してmappableDataを印刷し、実際にどのように表示されるかを確認することをお勧めします。

編集:コメントの議論に基づいて、mappableDataこの場合は実際にはJKDictionaryオブジェクトのコレクションです。(RestKitが使用しているJSON解析ライブラリ)でのサブクラスとしてJKDictionary定義されています。したがって、これは変更可能な辞書ではなく、を実装していませ。そのため、実行時にNSUnknownKeyExceptionが発生します。JSONKit.hNSDictionary[NSMutableDictionary setValue:forKey:]

あなたが望むものを達成するための1つの方法はそのようなものである可能性があります(テストされていません!):

- (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;
}
于 2011-07-18T06:25:04.983 に答える