iPhone SDK Objective-C __DATE__(编译日期)无法转换为NSDate [英] iPhone SDK Objective-C __DATE__ (compile date) can't be converted to an NSDate

查看:122
本文介绍了iPhone SDK Objective-C __DATE__(编译日期)无法转换为NSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Ok, I give up. Why would aDate sometimes return as nil?

如果我使用注释掉的行...或它们匹配的替换行,应该重要吗?

Should it matter if I use the commented-out lines... or their matching replacement lines?

推荐答案

如果手机的区域设置不是美国(或等效),则可以返回nil。

It can return nil if the phone's Region setting is not US (or equivalent).

尝试将格式化程序的区域设置设置为en_US:

Try setting the formatter's locale to 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆