Reputation: 207
In Swift you can initialize a variable in a class when you declare the variable:
var name: String = "John"
or you can initialize with init:
var name: String
init(name: String) {
self.name = name
}
Which version do you use and when?
Upvotes: 5
Views: 4909
Reputation: 72790
Unless you are providing the initial value as an initializer parameter, for which you have for obvious reasons do that in the initializer, you can use any of the 2 ways.
My rules are:
but besides the first one, the other 2 are just based on personal preference.
Upvotes: 4