ios Swift 中的 NSDate() 与 NSDate.date()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26628562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSDate() vs NSDate.date() in Swift
提问by max
I′m following along with the Bloc.io Swiftris tutorialwhere they initialize a date by:
我正在关注Bloc.io Swiftris 教程,他们通过以下方式初始化日期:
lastTick = NSDate.date()
Which causes a compile error:
这会导致编译错误:
'date()' is unavailable: use object construction 'NSDate()'
Which should equal:
这应该等于:
NSDate *lastTick = [NSDate date];
(from the NSDate reference)
(来自NSDate 参考)
Did Apple change the Swift interface to NSDate, since I have seen other examples that use NSDate.date
?
Apple 是否将Swift 接口更改为 NSDate,因为我看过其他使用 的示例NSDate.date
?
Is this just NSDate or can you not call type methods for any Objective-C APIs?
这只是 NSDate 还是您不能为任何 Objective-C API 调用类型方法?
回答by bandejapaisa
[NSDate date]
is a factory method for constructing an NSDate object.
[NSDate date]
是用于构造 NSDate 对象的工厂方法。
If you read the guide "Using Swift with Cocoa and Objective-C", there is a section on interacting with Objective-C apis:
如果您阅读了“将 Swift 与 Cocoa 和 Objective-C 一起使用”指南,其中有一节是关于与 Objective-C apis 交互的:
For consistency and simplicity, Objective-C factory methods get mapped as convenience initializers in Swift. This mapping allows them to be used with the same concise, clear syntax as initializers.”
Excerpt From: Apple Inc. “Using Swift with Cocoa and Objective-C.” iBooks. https://itun.es/gb/1u3-0.l
为了一致性和简单性,Objective-C 工厂方法被映射为 Swift 中的便利初始化器。这种映射允许它们使用与初始化程序相同的简洁、清晰的语法。”
摘自:Apple Inc. “将 Swift 与 Cocoa 和 Objective-C 结合使用”。电子书。https://itun.es/gb/1u3-0.l
So the factory method:
所以工厂方法:
[NSDate date]
is converted into an initializer in Swift
在 Swift 中被转换为初始化程序
NSDate()
It's not just NSDate where you will find this pattern, but in other Cocoa API's with factory methods.
您不仅可以在 NSDate 中找到这种模式,还可以在其他带有工厂方法的 Cocoa API 中找到这种模式。
回答by ChikabuZ
It seems that NSDate()
is new syntax. Previously, NSDate.date()
worked,
这似乎NSDate()
是新的语法。以前NSDate.date()
工作过,
but now you should useNSDate()
但现在你应该使用NSDate()
回答by ZeMoon
In Objective-C, [NSDate date]
simply calls [[NSDate alloc] init]
. Hence, you do not need to call NSDate.date()
in Swift. Simply calling NSDate()
will initialise a date object with the current date.
在 Objective-C 中,[NSDate date]
只需调用[[NSDate alloc] init]
. 因此,您不需要NSDate.date()
在 Swift 中调用。简单地调用NSDate()
将使用当前日期初始化日期对象。
回答by The Paramagnetic Croissant
Did Apple change the Swift interface to
NSDate
?
Apple 是否将 Swift 界面更改为
NSDate
?
Yes, they did. For me, the compiler tells:
是的,他们做了。对我来说,编译器告诉:
Foundation.NSDate:3:26: note: 'date()' has been explicitly marked unavailable here:
@objc(date) class func date() -> Self!
Foundation.NSDate:3:26: 注意:'date()' 已在此处明确标记为不可用:
@objc(date) class func date() -> Self!
So it is explicitly marked unavailable to Swift. This means that it has officially been deprecated.
所以它被明确标记为 Swift 不可用。这意味着它已被正式弃用。