为什么单音类值为零?

时间:2017-02-12 16:48:45

标签: swift swift3

在我的代码中发生了一些奇怪的事情,我无法理解为什么。

打印结果始终为零

这是我做的非常简单

ConditionHolder.instance.condition?.type = ConditionType.fixed
ConditionHolder.instance.condition?. ifoDateAndTime = dateTime
print("conditonvc1=\(ConditionHolder.instance.condition?.type)")
print("conditonvc2=\(ConditionHolder.instance.condition?.ifoDateAndTime?)")

这是我的班级

class ConditionHolder {

   static let instance = ConditionHolder()

    var condition: Condition?
}

和条件类

class Condition {

    var ifoDateAndTime: IFODateTime?
    var period: Period?
    var type: String?
}

1 个答案:

答案 0 :(得分:1)

您需要像这样初始化condition属性:

class ConditionHolder {

    static let instance = ConditionHolder()   
    let condition = Condition()

}

或者这个:

class ConditionHolder {

    static let instance = ConditionHolder()

    let condition: Condition

    init() {
        self.condition = Condition()
    }

}