NSDateFormatter for 2015-02-13 17:16:00 +0000

时间:2015-01-14 18:18:46

标签: ios nsdate nsdateformatter

NSString *value = [newProperties objectForKey:key]
NSLog(@"%@",value);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss zzzz"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate *dateFromString = [dateFormatter dateFromString:value];

2015-02-13 17:16:00 +0000
但是我在最后一行遇到了崩溃:

'-[__NSTaggedDate length]: unrecognized selector sent to instance 0xe41ba8e68d000000'

1 个答案:

答案 0 :(得分:4)

该错误清楚地表明value已经是NSDate,而不是NSString

您的所有代码都可以替换为一行:

NSDate *value = [newProperties objectForKey:key];

您可以通过记录其类来验证value是否为日期:

NSLog(@"value class = %@", [value class]);

作为旁注。如果您确实拥有所示格式的日期字符串,则您的格式说明符必须为yyyy-MM-dd HH:mm:ss Z