iPhone 上的 NSDate 初始化程序问题
我正在尝试在 iPhone 上使用日期进行一些基本操作。我想将“2010-02-19”这样的字符串转换为“Friday, Feb 19”这样的字符串。我收到的字符串总是在 2010-02-19。现在,我正在运行以下有效的代码,但它会产生一个警告,指出“NSDate 可能不会响应 +dateWithNaturalLanguageString:”
NSDate *date = [NSDate dateWithNaturalLanguageString:scheduled];
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@"EEEE, MMM dd"];
return [df stringFromDate:date];
该代码有效,但它会产生该警告。我应该担心这个吗?或者这在 iPhone 上可以吗?我应该使用另一个 NSDate 初始化程序吗?
I am trying to do some basic operations with Dates on the iPhone. I want to take a string like "2010-02-19" and turn it into a string like "Friday, Feb 19." The strings I will be receiving will always be in 2010-02-19. Right now, I am running the following code that works, but it produces a warning that says "NSDate may not respond to +dateWithNaturalLanguageString:"
NSDate *date = [NSDate dateWithNaturalLanguageString:scheduled];
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@"EEEE, MMM dd"];
return [df stringFromDate:date];
The code works, but it produces that warning. Should I worry about it, or is this OK on the iPhone. Is there another NSDate initializer that I should be using?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
+dateWithNaturalLanguageString
似乎仅在 Mac 上可用:Mac 开发中心 中的 NSDate 类参考与 iPhone 开发中心 中的 NSDate 类参考另外,文档还指出:
在类参考的概述部分,您可以找到以下信息:
鉴于这仅在 Mac 上正式可用,而在 iPhone 上不可用,因此我不会在 iPhone 项目中使用它。即使在 Mac 上,Apple 似乎也建议使用
NSDateFormatter
来代替。+dateWithNaturalLanguageString
seems to be only available on the Mac : NSDate Class Reference in Mac Dev Center vs. NSDate Class Reference in iPhone Dev CenterAlso, the documentation says that:
In the overview section of the class reference you can find the following information:
Given that this is only officially available on the Mac, but not on the iPhone, I wouldn't use it in an iPhone project. Even on the Mac it seems that Apple recommends using an
NSDateFormatter
instead.尝试使用 NSDateFormatter:dateFromString 。这里有一个示例。
Try using NSDateFormatter:dateFromString . There's an example here.