Search code examples
swiftallocation

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


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

I used this syntax in objectiveC, but in 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)"
    }
}

Solution

  • You can't do this in swift. You need to add separate line for this

    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)"
    

    }