How properly declare a variable in Swift?

Viewed 6779

I found quite interesting these different ways to declare a variable in Swift:

// METHOD 1
var dogName: String = "Charlie"

// METHOD 2
var dogName: String {
    return "Charlie"
}

// METHOD 3
let dogName = {
    return "Charlie"
}

// METHOD 4
var dogName: String = {
    return "Charlie"
}()

Obviously the method 3 declare a let and we known the difference; but why Swift allows the method 4?

What's the difference between these four methods?

I'm quite confusing in particular between method 2 and 4. In addition why the method 3 lose the final brackets compared to method 4?

4 Answers
Related