'init'是该类型的成员;使用'type(of:...)'来初始化一个新对象

时间:2017-02-06 15:16:25

标签: swift2 swift3

我在Swift 2中有这个代码,现在它在Swift 3中有错误:

错误是:

  

'init'是该类型的成员;使用'type(of:...)'来初始化a   相同动态类型的新对象

extension Date
{

    init(dateString:String) {
        let dateStringFormatter = DateFormatter()
        dateStringFormatter.dateFormat = "yyyy-MM-dd"
        dateStringFormatter.locale = Locale(identifier: "en_US_POSIX")
        let d = dateStringFormatter.date(from: dateString)!

        // error is here : 'init' is a member of the type; use 'type(of: ...)' to initialize a new object of the same dynamic type
        (self as NSDate).init(timeInterval:0, since:d) }
}

1 个答案:

答案 0 :(得分:0)

尝试super.init(timeInterval:0, since:d)