我试图将我的代码转换为Swift 3.0,并且在使用迁移工具后我无法转换某个代码块.
以前的SWIFT 3.0迁移:
import Foundation
extension NSDate {
convenience init(posixTime: Double) {
self.init(timeIntervalSince1970: Double(posixTime) / 1000.0)
}
}
Run Code Online (Sandbox Code Playgroud)
迁移工具之后:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
我按照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)
}
}
Run Code Online (Sandbox Code Playgroud)
然后Xcode要求我在"of:"前添加一个逗号,这会导致更多错误(以及更多将逗号放在错误位置的建议).在Swift 3中使用这段代码的正确方法是什么?
您可以轻松地convenience从init方法中删除它.
extension Date {
init(posixTime: Double) {
self.init(timeIntervalSince1970: Double(posixTime) / 1000.0)
}
}
Run Code Online (Sandbox Code Playgroud)
似乎迁移工具self在以前的代码中错误地将实例转换为NSDate.
| 归档时间: |
|
| 查看次数: |
397 次 |
| 最近记录: |