Swift - Custom setter on property

Solution 1:

You can't use set like that because when you call self.document = newValue you're just calling the setter again; you've created an infinite loop.

What you have to do instead is create a separate property to actually store the value in:

private var _document: UIDocument? = nil
var document: UIDocument? {
    get {
        return self._document
    }
    set {
        self._document = newValue
        useDocument()
    }
}

Solution 2:

Here's a Swift 3 version

var document : UIDocument? {
    didSet {
        useDocument()
    }
}