Articles, podcasts and news about Swift development, by John Sundell.

Using property observers

Published on 13 Dec 2018
Basics article available: Properties

I really love using property observers in Swift — makes it easy to write much more reactive code, with objects that automatically react to changes in state and other values.

class ChartTableViewCell: UITableViewCell {
    var values: [CGFloat] = [] {
        // Property observers let us easily react to changes
        // in property values to, for example, re-render a
        // part of our UI:
        didSet { renderChart() }
    // Superclass properties can also be observed, simply by
    // overriding one and adding an observation:
    override var accessoryView: UIView? {
        // 'oldValue' is automatically available within
        // didSet property observers:
        didSet { accessoryViewDidChange(from: oldValue) }

Support Swift by Sundell by checking out this sponsor:

Genius Scan SDK

Genius Scan SDK: Add a powerful document scanner to any iOS app. Genius Scan’s SDK features advanced image processing that’s the result of over 10 years of research and development and can be fully customized and integrated into your app with just one line of code. Mention Swift by Sundell when requesting a free demo to get a 20% discount on your license for a whole year.