//NSString *compileDate = [NSString stringWithFormat:@"%s", __DATE__];
NSString *compileDate = [NSString stringWithUTF8String:__DATE__];
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@"MMM d yyyy"];
//[df setDateFormat:@"MMM dd yyyy"];
NSDate *aDate = [df dateFromString:compileDate];
好吧,我放弃了。为什么 aDate 有时会返回 nil?
如果我使用注释掉的行...或其匹配的替换行,应该重要吗?
最佳答案
如果手机的区域设置不是美国(或同等设置),它可能返回 nil。
尝试将格式化程序的区域设置设置为 en_US:
NSString *compileDate = [NSString stringWithUTF8String:__DATE__];
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@"MMM d yyyy"];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[df setLocale:usLocale];
[usLocale release];
NSDate *aDate = [df dateFromString:compileDate];
关于iPhone SDK Objective-C __DATE__ (编译日期)无法转换为 NSDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2862469/