Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用RestKit将日期组件字典映射到NSDate_Ios_Json_Restkit - Fatal编程技术网

Ios 如何使用RestKit将日期组件字典映射到NSDate

Ios 如何使用RestKit将日期组件字典映射到NSDate,ios,json,restkit,Ios,Json,Restkit,我正在使用RestKit处理所有JSON响应。其中一个对象是日期,但它不是使用标准格式,而是一个具有日期、月份和年份参数的对象,如下所示 { "date": { "day": 30, "month": 7, "year": 2014 } } 如何构建一个RKMapping或RKValueTransformer,将其映射到一个NSDate对象?以下是我最后使用的 RKValueTransformer *dateTransformer = [RKBlockValue

我正在使用RestKit处理所有JSON响应。其中一个对象是日期,但它不是使用标准格式,而是一个具有日期、月份和年份参数的对象,如下所示

{
  "date": {
    "day": 30,
    "month": 7,
    "year": 2014
  }
}

如何构建一个
RKMapping
RKValueTransformer
,将其映射到一个
NSDate
对象?

以下是我最后使用的

RKValueTransformer *dateTransformer = [RKBlockValueTransformer valueTransformerWithValidationBlock:^BOOL(__unsafe_unretained Class sourceClass, __unsafe_unretained Class destinationClass) {
    return ([sourceClass isSubclassOfClass:[NSDictionary class]] && [destinationClass isSubclassOfClass:[NSDate class]]);
} transformationBlock:^BOOL(id inputValue, __autoreleasing id *outputValue, Class outputValueClass, NSError *__autoreleasing *error) {
    RKValueTransformerTestInputValueIsKindOfClass(inputValue, [NSDictionary class], error);
    RKValueTransformerTestOutputValueClassIsSubclassOfClass(outputValueClass, [NSDate class], error);
        NSDateComponents *dc = [[NSDateComponents alloc] init];
    dc.day = [inputValue[@"day"] integerValue];
    dc.month = [inputValue[@"month"] integerValue];
    dc.year = [inputValue[@"year"] integerValue];
    dc.calendar = [NSCalendar currentCalendar]; // This step is easy to forget.
    NSDate *aDate = dc.date;
    *outputValue = aDate;

    return YES;
}];
要真正使用这个变压器

[[RKValueTransformer defaultValueTransformer] addValueTransformer:dateTransformer];

NSDateComponents和NSCalendar。我非常熟悉
NSDateComponents
NSCalendar
。但是,我的问题是如何在Restkit中设置
RKMapping
RKValueTransformer
。您是否尝试过为
NSDictionary
NSDate
创建值转换器?或者使用具有
NSDate
accessor方法的自定义对象……问题是我不知道如何创建一个RKValueTransformer来实现这一点。不过我想出来了。