我有我試圖用NSDateFormatter()
轉換Unix時間戳,但是當我試圖通過我的NSDate
變量,我得到一個錯誤:Cannot assign value of type 'String' to type 'NSDate?'
的NSDate格式化錯誤
var releaseDate: NSDate?
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let seconds = Double(self.userA.releaseDate)
let timestampDate = NSDate(timeIntervalSince1970: seconds)
let stringTimeStamp = timestampDate.description
print(stringTimeStamp) // 2016-07-29 04:49:54 +0000
let releaseDateFormatter = NSDateFormatter()
releaseDateFormatter.dateFormat = "hh:mm:ss a"
releaseDate = releaseDateFormatter.stringFromDate(timestampDate)
}
我很困惑,因爲沒」我明確宣佈timestampDate
爲NSDate
?
stringFromDate實際上將Date轉換爲String。你可能需要dateFromString,它會從String對象中返回一個Date對象。 – Shad