Last active
November 4, 2015 19:14
-
-
Save tkrajacic/52f77a5c1fa156aea201 to your computer and use it in GitHub Desktop.
Why is the property observer called if no assignment takes place?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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