iOS RestKit日期解析

iOS RestKit日期解析,第1张

概述在一个旧的iOS项目中,我不得不解析dd / MM / yyyy格式的日期,所以我将这些行放在AppDelegate的静态方法中,并按预期工作 // Set the Dateformatter for the Dates returned by Knowledge tt/mm/yyyyNSDateFormatter *dateFormatter = [[NSDateFormatter alloc 在一个旧的iOS项目中,我不得不解析dd / MM / yyyy格式的日期,所以我将这些行放在AppDelegate的静态方法中,并按预期工作

// Set the Dateformatter for the Dates returned by KNowledge tt/mm/yyyyNSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];[dateFormatter setDateFormat:@"dd/MM/yyyy"];[[RKValuetransformer defaultValuetransformer] insertValuetransformer:dateFormatter atIndex:0];

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

对于这个日期“ExamDate”:“20.06.2014”我得到(NSDate *)_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调用Value transformer来实现这一点,Value transformer是映射数据时使用的.

这是你可以用来实现你想要的东西:

[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,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];
总结

以上是内存溢出为你收集整理的iOS RestKit日期解析全部内容,希望文章能够帮你解决iOS RestKit日期解析所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1057912.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存