Type does not have a member
Solution 1:
You cannot initialize an instance class property referencing another instance property of the same class, because it's not guaranteed in which order they will be initialized - and swift prohibits that, hence the (misleading) compiler error.
You have to move the initialization in a constructor as follows:
let components: NSDateComponents
init() {
self.components = myCalendar.components(.CalendarUnitYear | .CalendarUnitMonth, fromDate: now)
}
Solution 2:
I agree with @Antonio
The other way might be to create struct
if you don't want to use init
:
class DataModel {
struct MyStruct {
static var myCalendar:NSCalendar = NSCalendar.autoupdatingCurrentCalendar()
static let now = NSDate()
}
var myData = [NSDate : Float]()
var components = MyStruct.myCalendar.components(.CalendarUnitYear | .CalendarUnitMonth, fromDate: MyStruct.now)
}
Test
var model:DataModel = DataModel()
var c = model.components.year // 2014