Swift: how to change a property's value without calling its didSet function Swift: how to change a property's value without calling its didSet function swift swift

Swift: how to change a property's value without calling its didSet function


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


What you do in Objective-C to "avoid side effects" is accessing the backing store of the property - its instance variable, which is prefixed with underscore by default (you can change this using the @synthesize directive).

However, it looks like Swift language designers took specific care to make it impossible to access the backing variables for properties: according to the book,

If you have experience with Objective-C, you may know that it provides two ways to store values and references as part of a class instance. In addition to properties, you can use instance variables as a backing store for the values stored in a property.

Swift unifies these concepts into a single property declaration. A Swift property does not have a corresponding instance variable, and the backing store for a property is not accessed directly. (emphasis is mine)

Of course this applies only to using the "regular language" means, as opposed to using reflection: it might provide a way around this restriction, at the expense of readability.


If you exactly know when you want to apply side effects just make it explicitly:

1 Solution:

func noside(newValue : Int) {    hoodwink = newValue}func withside(newValue : Int) {    self.hoodwink = newValue    toggle = !toggle}

2 Solution:

var toggle : Bool = falsevar hoodwink : Int = 0 var hoodwinkToggle: Int {    get { return hoodwink }    set(newValue) {        hoodwink = newValue        toggle = !toggle    }}
  1. func setHoodwinkWithToggle(hoodwink: Int) {...}
  2. ....

I think these solutions will be more clear and readable, then using one variable which at some calls should have side effects and shouldn't at others.