我将函数today()
作为Int返回日期。但我想将此值作为整数分配给变量theDate
。这是我的代码:
以下是获取日期:
import UIKit
class ViewController: UIViewController {
let date = NSDate()
let formatter = DateFormatter()
let calendar = NSCalendar.current
现在我做了一些事情来隔离日期编号。出于某种原因,我必须在一个函数中执行此操作。所以这就是:
func today() -> Int {
let x: Int = calendar.component(.day, from: date as Date)
return x
}
但现在当我let theDate = today()
时,它会出错
如何将其分配给变量theDate
?
如果您不知道与上述方法相关的解决方案,那么将今天的日期作为整数放入变量的任何其他方法也可以,因为这就是我想要的。我正在使用Swift 3.