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