Make code run only once with lazy Properties.

Lazy Properties has attribute, allocate once when it called.

So we can make one time code with lazy Properties.

lazy var once: Void = {
    // Run code once
}()

It’s acting like this.

var ifFirst = false
if !isFirst {
    isFirst = true
    // Run code once
}


If you want read-only lazy Properties, here is the code.

private(set) lazy var once: Void = {
    // Run code once
}()

This property has basically internal scope but ‘Set’ is controlled like private.