iPhone SDK Objective-C __DATE__ (编译日期)无法转换为 NSDate

标签 iphone date compilation nsdate nsdateformatter

//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/

相关文章:

R:第n行第(m+1)列与第(n+1)行第m列之间的日期差

iphone - 接收者类型 *** 例如消息是前向声明

iphone - 隐藏后navigationController工具栏不会重新出现

iphone - AdMob 错误 - 在调用 loadRequest : 之前必须设置 GADBannerView 的 rootViewController 属性

iphone - NSOperation 和 NSOperationQueue 如何使用以及在哪里使用 >

php - 使用PHP获取目录中文件的最后修改日期

sql - 如何将日期和时间组合成 db2 中的时间戳?

c++ - 如何使用 OpenSSL 编译一个简单的程序?

c - GCC 编译器 - 代码块和 Mac OSX (XCode) 之间的潜在差异?

linux - 编译和安装内核时出错?