NSDate格式化程序 for 2015-02-13 17:16:00 +0000.


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'

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

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

NSDate *value = [newProperties objectForKey:key];

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

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

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

最新更新