类型没有成员

时间:2023-01-23 22:14:53

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 a constant defined three lines earlier. Here is the code:

我在一个快速运动的操场上玩,准备上一节新课。由于某些原因,我不断地得到一个错误,即类“没有成员类型”,前面三行定义了常量的名称。这是代码:

import Foundation
class DataModel {
    let myCalendar = NSCalendar.autoupdatingCurrentCalendar()

    var myData = [NSDate : Float]()
    let now  = NSDate()
    let components = myCalendar.components(.CalendarUnitYear | .CalendarUnitMonth, fromDate: now)
}

Xcode Beta6 keeps give me an error on the second to last line, saying that "DataModel.Type does not have a member named 'myCalendar'

Xcode Beta6在倒数第二行始终给我一个错误,说“DataModel”。类型没有名为“myCalendar”的成员

Though I don't think it should make a difference, I have tried defining myCalendar as a var.

虽然我不认为它应该有什么不同,但我已经尝试将myCalendar定义为一个var。

2 个解决方案

#1


9  

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.

您不能初始化引用同一类的另一个实例属性的实例类属性,因为它不能保证初始化的顺序—swift禁止这样做,因此(误导)编译器错误。

You have to move the initialization in a constructor as follows:

您必须在构造函数中移动初始化,如下所示:

let components: NSDateComponents

init() {
    self.components = myCalendar.components(.CalendarUnitYear | .CalendarUnitMonth, fromDate: now)
}

#2


1  

I agree with @Antonio The other way might be to create struct if you don't want to use init:

我同意@Antonio另一种方法是创建struct如果你不想使用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

#1


9  

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.

您不能初始化引用同一类的另一个实例属性的实例类属性,因为它不能保证初始化的顺序—swift禁止这样做,因此(误导)编译器错误。

You have to move the initialization in a constructor as follows:

您必须在构造函数中移动初始化,如下所示:

let components: NSDateComponents

init() {
    self.components = myCalendar.components(.CalendarUnitYear | .CalendarUnitMonth, fromDate: now)
}

#2


1  

I agree with @Antonio The other way might be to create struct if you don't want to use init:

我同意@Antonio另一种方法是创建struct如果你不想使用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