我做了一个简单的倒计时应用程序,我得到了这个错误的消息:无法调用非功能类型“日历”的值.
我需要改变什么?
@IBOutlet weak var deteLabelOutlet: UILabel!
let formatter = DateFormatter()
let useCalendar = NSCalendar.current()
let requesedComponent: NSCalendar.Unit = [
NSCalendar.Unit.month,
NSCalendar.Unit.day,
NSCalendar.Unit.hour,
NSCalendar.Unit.minute,
NSCalendar.Unit.second
]
override func viewDidLoad() {
super.viewDidLoad()
let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(printTime), userInfo: nil, repeats: true)
timer.fire()
}
func printTime() {
formatter.dateFormat = "MM/dd/yy hh:mm:ss a"
let startTime = Date()
let endTime = formatter.date(from: String("12/25/16 12:00:00 a"))
let timeDiffrenece = useCalendar.components(requesedComponent,fromDate: startTime, toDate: endTime!, options: [] )
deteLabelOutlet.text = "\(timeDiffrenece.month)Month \(timeDiffrenece.day) days \(timeDiffrenece.minute) minutes \(timeDiffrenece.second) seconds"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
最佳答案 在Swift 3中,当前日历不是一个功能.删除括号.
无论如何,强烈建议使用其他答案中建议的本机结构日历
let useCalendar = Calendar.current
编辑:
在将值分配给标签时,您必须解开所有日期组件,因为在Swift 3中,所有日期组件都是可选的.但是,由于指定了所有相关组件,因此可以强制安全地拆开组件.
deteLabelOutlet.text = "\(timeDiffrenece.month!)Month \(timeDiffrenece.day!) days \(timeDiffrenece.minute!) minutes \(timeDiffrenece.second!) seconds"