错误 - 实例成员不能用于类型自定义类

时间:2015-12-17 03:02:00

标签: swift swift2

我试图获得今天的约会

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'

1 个答案:

答案 0 :(得分:2)

您无法在实例化时访问日历属性:

试试这样:

class Date {
    let calendar = NSCalendar.currentCalendar()
    var day: Int {
       return  calendar.component(.Day, fromDate: NSDate())
    }
}


print(Date().day)   // 17