Skip to content

Instantly share code, notes, and snippets.

@tkrajacic
Last active November 4, 2015 19:14
Show Gist options
  • Save tkrajacic/52f77a5c1fa156aea201 to your computer and use it in GitHub Desktop.
Save tkrajacic/52f77a5c1fa156aea201 to your computer and use it in GitHub Desktop.
Why is the property observer called if no assignment takes place?
var value : String = "Test" {
didSet {
print("Changed Item Value")
}
}
let newValue = "Test"
private func changeIfNew<T: Comparable>(inout current: T, new: T) {
// This evaluates to false (as current == new) and the loop is skipped yet the property observer is still triggered
if current != new {
print("current \(current) - new: \(new)")
current = new
}
}
// If this is not called the observer is not triggered
changeIfNew(&value, new: newValue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment