Swift

Static Properties

Make the variables belong!

Shared Properties

In Swift, we have seen that we can create methods and properties that belong to a certain type, rather than its own instances. This is helpful for organization of your code. These types of properties are called static properties. It is super easy to create this. Let’s make a car structure.

struct Car {
    static var wheels = 4
    var color: String
    var model: String
}
    
let tesla = Car(color: "Red", model: "S")
print(tesla.wheels)

All objects that have the Car class will have 4 wheels, the same number. Static methods and variables stick to the struct, not each instance. If you were to change the value to say 5, all objects of the Car Class would have 5 wheels!!! :)

chevron_up