有没有办法让 Xcode 的调试器在本地时区(即,不是 UTC)中显示日期?

Gus*_*usP 5 debugging xcode timezone objective-c nsdate

我正在尝试调试大量使用日期的代码,这让我在调试器中比较了大量不同的NSDate值。调试器以 UTC 格式显示这些日期——例如:

date1 = (NSDate *) 0x01b11460 @"2012-02-15 18:55:00 +0000"

如果它会在我的本地时区中显示它们对我来说会容易得多,因为这就是我正在调试的测试代码似乎正在使用的。

我确实觉得我在这里错过了一些更基本的东西,所以我希望有人能启发我。提前致谢。

Gus*_*usP 1

最后,最有效的方法实际上是为 NSDate 添加一个类别,它只是重写该description方法以返回代表我当前时区的 NSDate 的字符串。我也只将其设置为“调试”,因为我在调试时确实只需要此覆盖。

这是我使用的 .h 文件:

#ifdef DEBUG

@interface NSDate (DebugHelper)

-(NSString *)description;

@end

#endif
Run Code Online (Sandbox Code Playgroud)

和 .m 文件:

#ifdef DEBUG

#import "NSDate+DebugHelper.h"

@implementation NSDate (DebugHelper)

-(NSString *) description
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateStyle:NSDateFormatterShortStyle];
    return [dateFormatter stringFromDate:self];
}

@end

#endif
Run Code Online (Sandbox Code Playgroud)

感谢 Jim Hayes 和 jrturton 的讨论和想法得出了这个答案。