-2
我得到了一個名爲錯誤:錯誤:二進制運算符'<='不能應用於'Int?'類型的操作數和 'INT'
Binary operator '<=' cannot be applied to operands of type 'Int?' and 'Int'
在線:if differenceOfDate.second <= 0
能有人請幫助!
let fromDate = Date(timeIntervalSince1970: TimeInterval(truncating: posts[indexPath.row].postDate))
let toDate = Date()
var calendar = Calendar.current
let components = Set<Calendar.Component>([.second, .minute, .hour, .day, .weekOfMonth, .month])
let differenceOfDate = Calendar.current.dateComponents(components, from: fromDate, to: toDate)
if differenceOfDate.second <= 0 {
cell.DateLbl.text = "now"
}else if differenceOfDate.second > 0 && differenceOfDate.minute == 0 {
cell.DateLbl.text = "\(differenceOfDate.second)s."
}
else if differenceOfDate.minute > 0 && differenceOfDate.hour == 0 {
cell.DateLbl.text = "\(differenceOfDate.minute)m."
}
else if differenceOfDate.hour > 0 && differenceOfDate.day == 0 {
cell.DateLbl.text = "\(differenceOfDate.hour)h."
}
else if differenceOfDate.day > 0 && differenceOfDate.weekOfMonth == 0 {
cell.DateLbl.text = "\(differenceOfDate.day)d."
}
else if differenceOfDate.weekOfMonth > 0 && differenceOfDate.month == 0 {
cell.DateLbl.text = "\(differenceOfDate.weekOfMonth)w."
}
else if differenceOfDate.month > 0 {
cell.DateLbl.text = "\(differenceOfDate.month)m."
}
請查找了資料https://developer.apple .com/documentation/foundation/calendar/2292887-datecomponents,你會發現它使用'Date'類型的參數,而不是'NSDate'。 –
相關:https://stackoverflow.com/questions/41198803/how-to-convert-from-nsdatetimeintervalsince1970-to-nsdate。 –
如果我使用Date來比錯誤「Binary operator'<='不能應用於類型'Int?'的操作數和'Int'「on line'if differenceOfDate.second <= 0' – Danjhi