Search code examples
iosswiftdidset

Is it possible to allow didSet to be called during initialization in Swift?


Question

Apple's docs specify that:

willSet and didSet observers are not called when a property is first initialized. They are only called when the property’s value is set outside of an initialization context.

Is it possible to force these to be called during initialization?

Why?

Let's say I have this class

class SomeClass {
    var someProperty: AnyObject {
        didSet {
            doStuff()
        }
    }

    init(someProperty: AnyObject) {
        self.someProperty = someProperty
        doStuff()
    }

    func doStuff() {
        // do stuff now that someProperty is set
    }
}

I created the method doStuff, to make the processing calls more concise, but I'd rather just process the property within the didSet function. Is there a way to force this to call during initialization?

Update

I decided to just remove the convenience intializer for my class and force you to set the property after initialization. This allows me to know didSet will always be called. I haven't decided if this is better overall, but it suits my situation well.


Solution

  • Create an own set-Method and use it within your init-Method:

    class SomeClass {
        var someProperty: AnyObject! {
            didSet {
                //do some Stuff
            }
        }
    
        init(someProperty: AnyObject) {
            setSomeProperty(someProperty)
        }
    
        func setSomeProperty(newValue:AnyObject) {
            self.someProperty = newValue
        }
    }
    

    By declaring someProperty as type: AnyObject! (an implicitly unwrapped optional), you allow self to fully initialize without someProperty being set. When you call setSomeProperty(someProperty) you're calling an equivalent of self.setSomeProperty(someProperty). Normally you wouldn't be able to do this because self hasn't been fully initialized. Since someProperty doesn't require initialization and you are calling a method dependent on self, Swift leaves the initialization context and didSet will run.