无法转换类型' String'的值预期参数类型' TimeInterval' (又名' Double')

时间:2017-05-15 03:39:51

标签: swift swift3 nsdate alamofire nsdateformatter

我正在格式化正在传递的时间。我收到了这个错误,我正在试图弄清楚它在这里发生了什么。我使用的是最新版本的Xcode(第8版),并在Swift 3中进行编程。这个错误意味着什么以及如何修复它?

我拼凑了与问题相关的部分代码,因此这里不会出现过多的信息。

var date: String {
    if _date == nil {
        _date = ""
    }
    return _date
}

init(weatherDict: Dictionary<String, AnyObject>){
    if let temp = weatherDict["temp"] as? Dictionary<String, AnyObject>
    {
        if let min = temp["min"] as? Double{
            self._lowTemp = ktof(kelvins: min)
        }

        if let max = temp["max"] as? Double{
            self._highTemp = ktof(kelvins: max)
        }
    }

    let unixConvertedDate = Date(timeIntervalSince1970: date)

    if let date = weatherDict["dt"] as? Double {
        let unixConvertedDate = Date(timeIntervalSince1970: date)
        let dateFormatter = DateFormatter()
        dateFormatter.dateStyle = .full
        //dateFormatter.dateFormat = "EEEE"
        dateFormatter.dateStyle = .none
        self._date = unixConvertedDate.dayoftheWeek()
    }
}

然后在课堂之外,所有上面的代码都在里面,我有这个。

extension Date {
func dayoftheWeek() -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "EEEE"
    return dateFormatter.string(from: self)
    }
}

1 个答案:

答案 0 :(得分:0)

            if let date = weatherDict["dt"] as? Double {

            let unixConvertedDate = Date(timeIntervalSince1970: date)
            let dateFormatter = DateFormatter()
            dateFormatter.dateStyle = .full
            dateFormatter.dateFormat = "EEEE"
            dateFormatter.timeStyle = .long
            self._date = "\(unixConvertedDate)"
        }

    }

}

extension Date {
    func dayOfTheWeek() -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "EEEE"
        return dateFormatter.string(from: self)
    }
}