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

Making weak or lazy properties readonly

Published on 28 Jul 2017
Basics article available: Properties

If you have a property in Swift that needs to be weak or lazy, you can still make it readonly by using private(set).

class Node {
    private(set) weak var parent: Node?
    private(set) lazy var children = [Node]()

    func add(child: Node) {
        children.append(child)
        child.parent = self
    }
}

Support Swift by Sundell by checking out this sponsor:

Bitrise

Bitrise: Rock-solid, fast continuous integration that’s super easy to configure. In just a few minutes, you can set up builds, tests, and automatic App Store and beta deployments for your project, all running in the cloud on every pull request and commit. Try it for free today.