2017-02-15 54 views
-1
func setTimeLeft() { let timeNow = NSDate() { 

    if timeEnd.compare(timeNow as Date) == ComparisonResult.orderedDescending { 
     let calendar = NSCalendar.current 
     let components = calendar.components([.day , .hour , .minute , .second] , fromDate: timeNow, toDate: timeEnd, options: []) 

     var dayText = String(components.day) + "d " 
     var hourText = String(components.hour) + "h " 

     // Hide day and hour if they are zero 
     if components.day <= 0 { 
      dayText = "" 
      if components.hour <= 0 { 
       hourText = "" 
      } 
     } 
     timeLeftLabel.text = dayText + hourText + String(components.minute) + "m " + String(components.second) + "s" 

    } else { 
     timeLeftLabel.text = "Ended" 
    } 
} 

我是新手,我只是停留在這個錯誤 錯誤在行如何解決「沒有更多上下文含糊不清的表達方式」?在SWIFT 3.0

let components = calendar.components([.day , .hour , .minute , .second] , fromDate: timeNow, toDate: timeEnd, options: []) 

回答

0

使用Calendar而不是NSCalendar因爲NSCalendar.current也會給Calendar實例,然後使用dateComponents(_:from:to:)從日曆中獲取DateComponents方法。

let calendar = Calendar.current 
let components = calendar.dateComponents([.day , .hour , .minute , .second], from: timeNow, to: timeEnd) 
+1

做到了,但同樣的錯誤仍然是有 – Bilal

+0

@Bilal你有沒有跟我更換你的代碼,什麼是錯誤?你能顯示屏幕快照如果可能的話。請注意,此方法中沒有可選參數。 –

+0

謝謝:)它的工作和錯誤刪除:) – Bilal

相關問題