iPhone 上的 NSDate 初始化程序问题

发布于 2024-08-21 13:25:53 字数 481 浏览 1 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绅士风度i 2024-08-28 13:25:53

+dateWithNaturalLanguageString 似乎仅在 Mac 上可用:Mac 开发中心 中的 NSDate 类参考与 iPhone 开发中心 中的 NSDate 类参考

另外,文档还指出:

它可能会带来意想不到的结果,并且
强烈建议不要使用它

在类参考的概述部分,您可以找到以下信息:

NSDate 提供了几种方法
解释并创建字符串
日期的表示(例如,
dateWithNaturalLanguageString:区域设置:
和descriptionWithLocale:)。在
一般情况,在 Mac OS X v10.4 及更高版本上
你应该使用一个实例
NSDateFormatter 解析并生成
使用方法的字符串
日期从字符串:和
stringFromDate:—参见 NSDateFormatter
Mac OS X 10.4 了解更多详细信息。

鉴于这仅在 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 Center

Also, the documentation says that:

It may give unexpected results, and
its use is strongly discouraged

In the overview section of the class reference you can find the following information:

NSDate provides several methods to
interpret and to create string
representations of dates (for example,
dateWithNaturalLanguageString:locale:
and descriptionWithLocale:). In
general, on Mac OS X v10.4 and later
you should use an instance of
NSDateFormatter to parse and generate
strings using the methods
dateFromString: and
stringFromDate:—see NSDateFormatter on
Mac OS X 10.4 for more details.

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.

雨落星ぅ辰 2024-08-28 13:25:53

尝试使用 NSDateFormatter:dateFromString 。这里有一个示例

Try using NSDateFormatter:dateFromString . There's an example here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文