Jay.Rana
Jay.Rana

Reputation: 59

How to use same value for updated variables until they take new value (Swift)

If i print the value of "self.q" . My programs prints the same value 10-12 times as it keeps getting updated. I want to print the value just once until the updated value is a new one. I am new to swift, not sure how to go about it.

some func{
 if (type == "q") {
                let json = JSON(receivedAsJSON)
                let receivedQ = json["q"].stringValue
                self.q = receivedQ
self.updateLabels()
print(self.q)

  }

 //function to update value received
func updateLabels() {
    qLabel.stringValue = self.q
    }

Upvotes: 2

Views: 156

Answers (1)

JaredH
JaredH

Reputation: 2398

I've renamed some variables just for clarity in this example. You could try something like this:

func someFunction() {

    let json = JSON(receivedAsJSON)
    let newValue = json["q"].stringValue
    updateLabels(newValue)

    // if the two values are different, then print the new value
    if (newValue != oldValue) {
        print("New value: \(newValue)")

        // update the oldValue so we can do the comparison next time.
        oldValue = newValue
    }
}

Upvotes: 2

Related Questions