iOS RestKit 日期解析 [英] iOS RestKit Date parsing

查看:50
本文介绍了iOS RestKit 日期解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In an old iOS project I had to parse dates that come in the format dd/MM/yyyy so I put these lines in a static method of my AppDelegate and it worked as expected

// 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];

然而在实际项目中我的日期格式略有不同dd.MM.yyyy,所以我使用了相同的代码行,只是切换了日期格式,但没有解析日期.

However in the actual projects my dates come in a slightly different format dd.MM.yyyy, so I used the same lines of code, just switched the Date format, but the date is not parsed.

对于这个日期 "ExamDate":"20.06.2014" 我得到 (NSDate *) _examDate = 0x08f7dcd0 2014-01-01 01:00:00 CET解析,我不明白为什么.

For this date "ExamDate":"20.06.2014" I get (NSDate *) _examDate = 0x08f7dcd0 2014-01-01 01:00:00 CET after parsing and I can't understand why.

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

Update: I made a small test with this code:

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

并进入日志:Date: 2014-06-19 22:00:00 +0000

推荐答案

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

It looks like your in a timezone with a GMT offset of -2:00. Set your timezone to 0 GMT to have it printed correctly. What we usually do is just use unix time when passing around dates so we can avoid this sort of issue.

dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];

由于您的问题似乎出在 RestKit 的解析中,因此您需要使用自己的日期解析器.您可以通过安装 RestKit 称为 Value Transformer 的东西来实现这一点,它在映射数据时使用.

Since your problem appears to be in RestKit's parsing you'll need to use your own date parser. You can do this by installing what RestKit calls a Value Transformer which is what it uses when mapping data.

您可以使用以下方法来实现您想要的:

Here's one that you can work with to achieve what you want:

[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 的支持.你应该可以尝试这样的事情:

Alternatively, it looks like RestKit has it's own NSDateFormatter category which adds support for adding it as an RKValueTransformer. You should be able to try something like this:

[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];

这篇关于iOS RestKit 日期解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆