我试图获得今天的约会
import Foundation
class Date {
var calendar = NSCalendar.currentCalendar()
var day = calendar.component(NSCalendarUnit.Day, fromDate: NSDate())
}
但我一直收到错误
Instance member 'calendar' cannot be used on type 'Date'
答案 0 :(得分:2)
您无法在实例化时访问日历属性:
试试这样:
class Date {
let calendar = NSCalendar.currentCalendar()
var day: Int {
return calendar.component(.Day, fromDate: NSDate())
}
}
print(Date().day) // 17