Search code examples
swiftpropertiescomputed-properties

Difference between computed property and property set with closure in swift 3?


I have read this (Difference between computed property and property set with closure) helpful question but it doesn't fully answer something i'd like to know.

It answers the difference between:

//closure
var pushBehavior: UIPushBehavior = {
    let lazilyCreatedPush = UIPushBehavior()
    lazilyCreatedPush.setAngle(50, magnitude: 50)
    return lazilyCreatedPush
}()

and

//computed
var pushBehavior: UIPushBehavior {
    get{
        let lazilyCreatedPush = UIPushBehavior()
        lazilyCreatedPush.setAngle(50, magnitude: 50)
        return lazilyCreatedPush
    }
}

I understand this. Now if we change the first one what is the difference between the computed variable in the second example and this:

var pushBehavior: UIPushBehavior {
    let lazilyCreatedPush = UIPushBehavior()
    lazilyCreatedPush.setAngle(50, magnitude: 50)
    return lazilyCreatedPush
}

To me this acts exactly like the get{} but without the get keyword. Is that the case and if so why do we have the get keyword?


Solution

  • There is no difference.

    From the documentation

    You can simplify the declaration of a read-only computed property by removing the get keyword and its braces.