Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.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日期解析_Ios_Restkit_Nsdateformatter - Fatal编程技术网

iOS RestKit日期解析

iOS RestKit日期解析,ios,restkit,nsdateformatter,Ios,Restkit,Nsdateformatter,在一个旧的iOS项目中,我必须解析格式为dd/MM/yyyyy的日期,因此我将这些行放在AppDelegate的静态方法中,它按预期工作 // Set the Dateformatter for the Dates returned by Knowledge tt/mm/yyyy NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd/MM/yyyy"];

在一个旧的iOS项目中,我必须解析格式为
dd/MM/yyyyy
的日期,因此我将这些行放在AppDelegate的静态方法中,它按预期工作

// Set the Dateformatter for the Dates returned by Knowledge tt/mm/yyyy
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];
[[RKValueTransformer defaultValueTransformer] insertValueTransformer:dateFormatter atIndex:0];
然而,在实际项目中,我的日期的格式略有不同,因此我使用了相同的代码行,只是切换了日期格式,但没有解析日期

对于这个日期“ExamDate”:“20.06.2014”我在解析后得到了
(NSDate*)\u ExamDate=0x08f7dcd0 2014-01-01 01:00:00 CET
,我不明白为什么

更新: 我用这段代码做了一个小测试:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd.MM.yyyy"];
NSDate *date = [dateFormatter dateFromString:@"20.06.2014"];
DLog(@"Date: %@", date);

并记录在日志中:
日期:2014-06-19 22:00:00+0000

看起来您所在的时区的GMT偏移量为-2:00。将时区设置为0 GMT以正确打印。我们通常只在传递日期时使用unix时间,这样就可以避免此类问题

dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
因为您的问题似乎是在RestKit的解析中,所以您需要使用自己的日期解析器。您可以通过安装RestKit所称的值转换器来实现这一点,它在映射数据时使用值转换器

这里有一个你可以用来实现你想要的:

[RKObjectMapping alloc]; // This ensures you will actually insert at index 0!

[RKValueTransformer.defaultValueTransformer
 insertValueTransformer:
 [RKBlockValueTransformer
  valueTransformerWithValidationBlock:^BOOL(__unsafe_unretained Class inputValueClass, __unsafe_unretained Class outputValueClass) {
      return (([inputValueClass isSubclassOfClass:[NSDate class]] && [outputValueClass isSubclassOfClass:[NSString class]]) ||
              ([inputValueClass isSubclassOfClass:[NSString class]] && [outputValueClass isSubclassOfClass:[NSDate class]]));
  }
  transformationBlock:^BOOL(id inputValue, __autoreleasing id *outputValue, __unsafe_unretained Class outputClass, NSError *__autoreleasing *error) {
      RKValueTransformerTestInputValueIsKindOfClass(inputValue, (@[ [NSString class], [NSDate class] ]), error);
      RKValueTransformerTestOutputValueClassIsSubclassOfClass(outputClass, (@[ [NSString class], [NSDate class] ]), error);
      if ([inputValue isKindOfClass:[NSString class]]) {
          // We're mapping from a string to a date.
          // You can add your formatter here.
          // Below is mine for mapping from Unix Time to an NSDate.
          NSString* input = inputValue;
          *outputValue = [NSDate dateWithTimeIntervalSince1970:input.integerValue];
      } else if ([inputValue isKindOfClass:[NSDate class]]) {
          // We're mapping from a date to a string.
          // You can add your formatter here (if needed).
          // Below is mine for mapping from an NSDate to Unix Time.
          NSDate* input = inputValue;
          *outputValue = @([input timeIntervalSince1970]);
      }
      return YES;
  }]
 atIndex:0]; // Insert at index 0 so your formatter is always used over the default one.
或者,它看起来像RestKit有自己的NSDateFormatter类别,该类别增加了对将其添加为RKValueTransformer的支持。您应该能够尝试以下内容:

[RKObjectMapping alloc]; // This ensures you will actually insert at index 0!
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd.MM.yyyy"];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
[RKDefaultValueTransformer insertValueTransformer:dateFormatter atIndex:0];

你说得对。。。这解决了我在dateformatter示例中的问题,但仍然无法在RestKit中自动解析,因此我仍然得到2014年1月1日的数据。您使用的是哪个版本的RestKit@HonsI目前使用RestKit 0.23。1@Hons,因为您使用的是0.23.1,所以可以使用RKValueTransformer。我已经在我的答案中加了一个。你需要在我评论的地方添加你自己的格式代码,但这应该会让你走上正轨。我将你的代码添加到我的项目中。。第一个块,即验证,为每个属性调用,对于my date,它返回YES,但第二个块从未调用