是否只有我的XCode / computer / iOS设备使用此代码获取nil?这太疯狂了!
NSDateFormatter *f = [NSDateFormatter new];
[f setDateFormat:@"yyyy-MM-dd"];
id ret = [f dateFromString:@"2014-10-05"];
NSLog(@"date = %@", ret);
这是我的控制台的日志:
2015-04-06 21:35:22.899 test [665:222261] date =(null)
顺便说一下:这是一个新项目,我可以在模拟器上和iPhone上同时使用。答案 0 :(得分:0)
与本地TimeZone相关的一些错误,请查看:
NSDateFormatter *f = [NSDateFormatter new];
NSTimeZone *local = [NSTimeZone localTimeZone];
NSLog(@"secondsFromGMT = %ld", [local secondsFromGMT]); // this is -14400
[f setTimeZone:local];
NSDate *ret = [f dateFromString:@"2014-10-05"];
NSLog(@"date = %@", ret); // this is nil
[f setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:-14400]];
[f setDateFormat:@"yyyy-MM-dd"];
ret = [f dateFromString:@"2014-10-05"];
NSLog(@"date = %@", ret); // this is 2014-10-05 04:00:00 +0000
我最终在我的代码中执行此操作:
NSDateFormatter *f = [NSDateFormatter new];
NSTimeZone *z = [NSTimeZone localTimeZone];
z = [NSTimeZone timeZoneForSecondsFromGMT:z.secondsFromGMT];
[f setTimeZone:z];
// use f here no problem