Learn Courses My Dashboard

Difference between var x: Type / var x = Type()

hello! Guys what’s the difference between declaring and initialization ?

Like, in the first example, I should to declare a value after, but why just not to put the = ?

Снимок экрана 2021-12-01 в 14.35.58

var model: Person
this is just declaring a variable called model of type Person.

var viewModel = ViewModel()
this is both declaring and initializing a variable called viewModel of type ViewModel and it is being initialized to a new instance of ViewModel

you could also have written
var viewModel: ViewModel = ViewModel()

and sometimes you do not want to initialize right away so you can just declare the variable without having to assign it right away