如何将函数的结果分配给成员变量并在 Swift 中的 if 语句中进行验证

How to alloc a result of function to the member variable and validate in if statement in Swift

如何将函数的结果分配给成员变量并在Swift

中的if语句中进行验证

我在 objectiveC 中使用了这个语法,但在 Swift?

class PointClass {
    var x: Int = 0
    var y: Int = 0
    var z: Bool = false

    init(x: Int, y: Int) {
        self.x = x
        self.y = y
    }

    func getBol() -> Bool {
        return true
    }

    func toString() -> String {

        if (z = getBol()) == true {//like objc

        }

        return "x=\(x), y=\(y)"
    }
}

您不能在 swift 中执行此操作。您需要为此添加单独的行

func getBol() -> Bool {
return true

}

func toString() -> String {

  self.z = getBol()
    if self.z { // if this will true. if statement will execute

    }

return "x=\(x), y=\(y)"

}