Implementation of a property and a corresponding animated setter There a various examples in UIKit where a class has a property and a corresponding method to set the property along with an animated property.

Examples include the progress and setProgress(_:animated:) of UIProgressView or the isEditing and setEditing(_:animated:) of UITableView.

In Objective-C you implement this by overriding the plain property setter to call the additional setter with the additional parameter:

- (void)setEditing:(BOOL)editing {
    [self setEditing:editing animated:NO];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    _editing = editing // set the instance variable

    // The rest of the code
}


The question is about doing this in Swift. The best I can come up requires using a private backing variable for the public computed property in addition to the corresponding animated setter.

private var _editing: Bool

public var isEditing: Bool {
    get { return _editing }
    set { setEditing(newValue, animated: false) }
}

public func setEditing(_ editing: Bool, animated: Bool) {
    _editing = editing

    // The rest of the code
}


Is there a better way to implement this pattern in Swift without the need to wrap a private property with a computed property à la Objective-C?
The alternative is: 

public var isEditing: Bool {
    didSet {
        // do whatever you need when the property changes, e.g.
        updateView()
    }
}

// wrap the changing of `isEditing` in some animation

func setEditing(_ editing: Bool, animated: Bool) {
    if animated {
        someAnimation {
            isEditing = editing
        }
    } else {
        isEditing = editing
    }
}


This basically separates the “do what I need when isEditing changes” from the “by the way, animate that” and it eliminates a private backing stored property.

There are cases where you need to do the “exposed computed property with a private backing stored property” approach, but I generally avoid that pattern where I can.