如何解决“没有更多上下文的歧义表达类型"?在 swift 3.0 [英] how to resolve “Type of expression in ambiguous without more context”? in swift 3.0
本文介绍了如何解决“没有更多上下文的歧义表达类型"?在 swift 3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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: [])
推荐答案
使用 Calendar
而不是 NSCalendar
因为 NSCalendar.current
也会给 Calendar
实例然后得到 DateComponents
使用 dateComponents(_:from:to:)
方法从日历中提取.
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)
这篇关于如何解决“没有更多上下文的歧义表达类型"?在 swift 3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文