What is the main difference between \"static var\" and \"var\" in Swift? Can someone explain this difference to me, possibly with a little example?
static var
belongs to type itself while var
belongs to instance (specific value that is of specific type) of type. For example:
struct Car {
static var numberOfWheels = 4
var plateNumber: String
}
Car.numberOfWheels = 3
let myCar = Car(plateNumber: "123456")
All cars has same amount of wheels. An you change it on type Car
itself.
In order to change plate number you need to have instance of Car
. For example, myCar
.