0

我有这个类别:

@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];  

它在模拟器上运行良好,但在设备上甚至没有调用类别方法,有什么想法吗?

4

1 回答 1

0

如果在调试器中添加符号断点,它会得到解决吗?如果是这样,执行会停止吗?

您是否将此代码编译到静态 .a 库中?GCC 链接器需要使用 -all_load 标志来确保获取静态库中的类别。

于 2012-05-15T19:41:41.693 回答