how to resolve “Type of expression in ambiguous without more context”? in swift 3.0

[亡魂溺海] 提交于 2019-12-12 03:08:49

问题


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"
    }
}

i'm newbie and i just stuck at this error error at line

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

回答1:


Use Calendar instead of NSCalendar because NSCalendar.current will also give Calendar instance and then get DateComponents from calendar using dateComponents(_:from:to:) method.

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


来源:https://stackoverflow.com/questions/42246152/how-to-resolve-type-of-expression-in-ambiguous-without-more-context-in-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!