Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iphonesdk Objective-C uuu DATE uuuu(编译日期)可以';不能转换为NSDate_Iphone_Date_Compilation_Nsdate_Nsdateformatter - Fatal编程技术网

iphonesdk Objective-C uuu DATE uuuu(编译日期)可以';不能转换为NSDate

iphonesdk Objective-C uuu DATE uuuu(编译日期)可以';不能转换为NSDate,iphone,date,compilation,nsdate,nsdateformatter,Iphone,Date,Compilation,Nsdate,Nsdateformatter,好吧,我放弃。为什么aDate有时会返回为零 如果我使用注释掉的行是否重要。。。或其匹配的替换线路?如果手机的区域设置不是US(或等效设置),则会返回nil 尝试将格式化程序的区域设置设置为en_US: //NSString *compileDate = [NSString stringWithFormat:@"%s", __DATE__]; NSString *compileDate = [NSString stringWithUTF8String:__DATE__]; NSDateForm

好吧,我放弃。为什么aDate有时会返回为零


如果我使用注释掉的行是否重要。。。或其匹配的替换线路?

如果手机的区域设置不是US(或等效设置),则会返回nil

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

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

稍微修改DyingCactus对启用ARC代码的回答(以便更容易地复制粘贴):


哇!我试试看。我以为日期总是在编译时确定的。。。基于我编译它的机器(英语)。所以日期总是以“2010年2月1日”的格式。(我不是要求在运行时确定日期…或基于用户的本地设置。)
\uuuu date\uuuu
宏在编译时确定,并替换为该格式的字符串文字。但将该字符串转换为NSDate是在受当前区域影响的运行时进行的。请注意,不值得回答:由于预处理器的工作方式,要将日期转换为NSString,只需编写@_DATE即可__
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];  
NSString *compileDate = [NSString stringWithUTF8String:__DATE__];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MMM d yyyy"];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[df setLocale:usLocale];
NSDate *aDate = [df dateFromString:compileDate];