【问题标题】:How to assign a value to optional property of class如何为类的可选属性赋值
【发布时间】:2020-03-14 20:42:38
【问题描述】:

我尝试了很多不同的方法来为 stepperValue 赋值,但都无济于事。我通过 Firebase 填充了我的进攻模型,我只想为每个进攻“分配”一个步进值,以便我可以进行计算,即点 X stepperValue。

模型 swift 文件:

class Offence : Comparable {

var section: String
var name: String
var cost: Int
var points: Int
var stepperValue: Double?

init(section: String, name: String, cost: Int, points: Int) {
    self.section = section
    self.name = name
    self.cost = cost
    self.points = points
}

class Variables {

static var selectedOffencesArray: [Offence] = [Offence]()

}

这个在自己的swift文件中的TableViewCell类中

@IBAction func stepperValueChanged(_ sender: Any) {

 for offence in Variables.selectedOffencesArray {
        offence.stepperValue! = 3.0

    }

【问题讨论】:

  • 在下面解决,但它一直返回 nil

标签: swift class properties optional stepper


【解决方案1】:

以下只是工作

for offence in Variables.selectedOffencesArray {
    offence.stepperValue = 3.0 // << no force (!) needed
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    • 2011-08-15
    • 1970-01-01
    • 2011-03-02
    • 2011-02-15
    相关资源
    最近更新 更多