我有这个类别:
@interface NSDate (PrettyDate)
- (NSString *)prettyDate;
@end
#import "NSDate+PrettyDate.h"
@implementation NSDate (PrettyDate)
- (NSString *)prettyDate
{
NSString *prettyTimestamp = nil;
float delta = [self timeIntervalSinceNow] * -1;
if (delta < 60) {
prettyTimestamp = @"Just now";
} else if (delta < 120) {
prettyTimestamp = @"One minute ago";
} else if (delta < 3600) {
prettyTimestamp = [NSString stringWithFormat:@"%d minutes ago", (int) floor(delta/60.0) ];
} else if (delta < 7200) {
prettyTimestamp = @"One hour ago";
} else if (delta < 86400) {
prettyTimestamp = [NSString stringWithFormat:@"%d hours ago", (int) floor(delta/3600.0) ];
} else if (delta < ( 86400 * 2 ) ) {
prettyTimestamp = @"One day ago";
} else if (delta < ( 86400 * 7 ) ) {
prettyTimestamp = [NSString stringWithFormat:@"%d days ago", (int) floor(delta/86400.0) ];
} else {
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
prettyTimestamp = [NSString stringWithFormat:@"On %@", [formatter stringFromDate:self]];
}
return prettyTimestamp;
}
@end
我这样称呼它:
NSDateFormatter *currentFormat = [[NSDateFormatter alloc] init];
currentFormat.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *date = [currentFormat dateFromString:dateString];
cell.detailTextLabel.text = [date prettyDate];
它在模拟器上运行良好,但在设备上甚至没有调用类别方法,有什么想法吗?