Type does not have a member

前端 未结 2 756
别那么骄傲
别那么骄傲 2020-11-27 22:31

I\'m playing around with a Swift playground working on a new class. For some reason I keep getting an error that the class \"does not have a member type\" with the name of

2条回答
  •  不知归路
    2020-11-27 23:10

    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)
    }
    

提交回复
热议问题