在我的代码中发生了一些奇怪的事情,我无法理解为什么。
打印结果始终为零
这是我做的非常简单
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?
}
答案 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()
}
}