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?
如果我使用注释掉的行...或其匹配的替换行,应该重要吗?
//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];
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果手机的区域设置不是美国(或同等设置),它可以返回 nil。
尝试将格式化程序的区域设置设置为 en_US:
It can return nil if the phone's Region setting is not US (or equivalent).
Try setting the formatter's locale to en_US:
稍微修改 DyingCactus 对 ARC 启用代码的答案(为了更容易复制粘贴):
Slightly modifying DyingCactus' answer for ARC enabled code (for easier copying-n-pasting):