How can you set a property's value in Swift, without calling its didSet()
function outside of an initialization context? The code below was a failed experiment to achieve this within the classes' noside()
function
class Test
{
var toggle : Bool = 0
var hoodwink : Int = 0 {
didSet(hoodwink)
{
toggle = !toggle
}
}
// failed attempt to set without a side effect
func noside(newValue : Int)
{
hoodwink = newValue
println("hoodwink: \(hoodwink) state: \(toggle)")
}
func withside(newValue : Int)
{
self.hoodwink = newValue
println("hoodwink: \(hoodwink) state: \(toggle)")
}
}
It is quite trivial to do in Objective-C with auto-synthesized properties:
With side effect (if present in setter):
self.hoodwink = newValue;
Without side effect:
_hoodwink = newValue;
A possible hack around this is to provide a setter which bypasses your didSet
var dontTriggerObservers:Bool = false
var selectedIndexPath:NSIndexPath? {
didSet {
if(dontTriggerObservers == false){
//blah blah things to do
}
}
}
var primitiveSetSelectedIndexPath:NSIndexPath? {
didSet(indexPath) {
dontTriggerObservers = true
selectedIndexPath = indexPath
dontTriggerObservers = false
}
}
Ugly but workable