如果我打印" self.q"的值。我的程序打印相同的值10-12次,因为它不断更新。我想只打印一次值,直到更新的值是新值。我是新手,不知道如何去做。
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
}
答案 0 :(得分:2)
为了清楚起见,我在这个例子中重命名了一些变量。你可以尝试这样的事情:
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
}
}