我正在尝试将我的代码转换为 Swift 3.0,并且在使用迁移工具后我很难转换某个代码块。
之前到 SWIFT 3.0 的迁移:
import Foundation
extension NSDate {
convenience init(posixTime: Double) {
self.init(timeIntervalSince1970: Double(posixTime) / 1000.0)
}
}
迁移后工具:
extension Date {
init(posixTime: Double) {
//ERROR IN THE LINE BELOW: "'init' is a member of the type; use 'type(of: ...)' to initialize a new object of the same dynamic type"
(self as NSDate).init(timeIntervalSince1970: Double(posixTime) / 1000.0)
}
}
我按照 Xcodes 的建议将“init”替换为“type(of:init)”:
extension Date {
init(posixTime: Double) {
//ERROR IN THE LINE BELOW: "Expected expression in list of expressions"
(self as NSDate).type(of: init)(timeIntervalSince1970: Double(posixTime) / 1000.0)
}
}
然后 Xcode 要求我在“of:”前面添加一个逗号,这会导致更多错误(以及更多将逗号放在错误位置的建议)。在 Swift 3 中使用这段代码的正确方法是什么?
最佳答案
您可以轻松删除 convenience
从初始化方法。
extension Date {
init(posixTime: Double) {
self.init(timeIntervalSince1970: Double(posixTime) / 1000.0)
}
}
似乎迁移工具错误地转换了
self
实例到您之前的代码中的 NSDate。
关于ios - NSDate 代码未迁移到 Swift 3.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39863373/