NSDateFormatter 的 init 方法已被弃用?

发布于 2024-09-08 10:17:29 字数 495 浏览 3 评论 0原文

根据

http://developer. apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html

NSDateFormatterinit 方法是“适用于 iPhone OS 2.0 至 iPhone OS 3.2",因此不适用于 4.0。现在,它确实有效,但这看起来很奇怪。这是一个错误还是有其他方法来创建 NSDateFormatter ?

Per

http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html

The init method of NSDateFormatter is "Available in iPhone OS 2.0 through iPhone OS 3.2", and therefore not in 4.0. Now, it certainly works, but this seems odd. Is this is a mistake or is there some other way to create a NSDateFormatter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

执妄 2024-09-15 10:17:29

这次弃用只是苹果清理了 NSDateFormatter 的标头。 init 已在 NSNumberFormatter 继承的 NSObject 中声明。不需要重新声明,但是在实现中,苹果将覆盖 init,因为子类应该为超类的默认初始化程序提供实现。

This deprecation is just apple cleaning up the headers for NSDateFormatter. init is already declared in NSObject which NSNumberFormatter inherits from. Redeclaring is not necessary, however in the implementation apple will override init as subclassess should provide implementations for the default initializer of the superclass.

画骨成沙 2024-09-15 10:17:29

就像falconcreek所说这里,苹果只是清除文档。

这意味着最初,- (id) init 已在 NSDateFormatter 头文件中重新声明。这是不必要的,后来有人把它删除了。该文档可能是自动生成的,并且没有发现只是已弃用的重新声明。

简而言之,随心所欲地使用 initNSDateFormatter

Like falconcreek said here, Apple is just clearing the docs.

That means that originally, - (id) init has been redeclared in the NSDateFormatter header file. That was unnecessary and somebody just removed it later. The documentation is probably generated automatically and didn't picked up that it was only the redecleration that was deprecated.

In short, use init as you wish with NSDateFormatter!

最终幸福 2024-09-15 10:17:29

我更相信头文件而不是文档。

例如, 格式化程序行为和操作系统版本似乎自相矛盾:

默认情况下,在 Mac OS X v10.4 上,NSDateFormatter 实例的行为与在 Mac OS X 版本 10.0 到 10.3 上的行为相同。在 Mac OS X v10.5 及更高版本上,NSDateFormatter 默认为 10.4+ 行为。

如果您使用 initWithDateFormat:allowNaturalLanguage: 初始化格式化程序,您将(出于向后兼容性的原因)创建一个“旧式”日期格式化程序。要使用新行为,请使用 init 初始化格式化程序。如有必要,您可以使用 setDefaultFormatterBehavior: 设置默认类行为),您可以使用带有参数 NSDateFormatterBehavior10_4setFormatterBehavior: 消息设置实例的行为代码>.

听起来 initWithDateFormat:allowNaturalLanguage: 实际上是已弃用的方法?

I would trust the header files over the documentation.

For example, Formatter Behaviors and OS Versions seems to contradict itself:

By default, on Mac OS X v10.4 instances of NSDateFormatter have the same behavior as they did on Mac OS X versions 10.0 to 10.3. On Mac OS X v10.5 and later, NSDateFormatter defaults to the 10.4+ behavior.

If you initialize a formatter using initWithDateFormat:allowNaturalLanguage:, you are (for backwards compatibility reasons) creating an “old-style” date formatter. To use the new behavior, you initialize the formatter with init. If necessary, you can set the default class behavior using setDefaultFormatterBehavior:), you can set the behavior for an instance using setFormatterBehavior: message with the argument NSDateFormatterBehavior10_4.

It sounds like initWithDateFormat:allowNaturalLanguage: is actually the deprecated method?

习ぎ惯性依靠 2024-09-15 10:17:29

这真的很奇怪。也许 +[NSDateFormatter new] 可以完成这项工作?

That’s really strange. Maybe +[NSDateFormatter new] will do the job?

送舟行 2024-09-15 10:17:29

我在多个 iOS4 应用程序中使用 init 方法,并且它们已获得批准并在商店中。我想不出有什么办法让它工作,所以这看起来只是文档上的一个错字。

I use the init method in an multiple iOS4 apps and they are approved and in the store. I can think of no way to get it to work so it would seem like its just a typo on the docs.

伴我心暖 2024-09-15 10:17:29

类引用中的示例代码仍然使用 init 方法,因此我认为在您自己的代码中使用它是安全的。

The example code in the class reference still uses the init method, so I'd say its safe to use it in your own code.

凝望流年 2024-09-15 10:17:29

您可以证明 NSDateFormatter 具有不同的行为,具体取决于您选择的初始化程序和您正在编译的 SDK。以下是使用SDK 10.5和10.6测试的。

NSDateFormatter* df = [[NSDateFormatter alloc] initWithDateFormat:@"yyyyMMdd" allowNaturalLanguage:NO];
if ([df formatterBehavior] == NSDateFormatterBehavior10_0)
    NSLog(@"NSDateFormatterBehavior10_0");
else if ([df formatterBehavior] == NSDateFormatterBehavior10_4)
    NSLog(@"NSDateFormatterBehavior10_4");

这将返回 NSDateFormatterBehavior10_0
相反,当您使用 [[NSDateFormatter alloc] init]; 时,行为是 NSDateFormatterBehavior10_4

You can proof yourself that NSDateFormatter has a different behaviour dependent on the initializer you choose and the SDK you are compiling with. The following is tested with SDK 10.5 and 10.6.

NSDateFormatter* df = [[NSDateFormatter alloc] initWithDateFormat:@"yyyyMMdd" allowNaturalLanguage:NO];
if ([df formatterBehavior] == NSDateFormatterBehavior10_0)
    NSLog(@"NSDateFormatterBehavior10_0");
else if ([df formatterBehavior] == NSDateFormatterBehavior10_4)
    NSLog(@"NSDateFormatterBehavior10_4");

This returns NSDateFormatterBehavior10_0.
Instead, when you use [[NSDateFormatter alloc] init]; the behaviour is NSDateFormatterBehavior10_4.

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