Struct : Value Type | No Inheritance | No Deinitializers | No Type casting
Class : Reference Type | Inheritance supported | Has Deinitializers | Type casting
Common factors between struct and class:
Both allows to define properties to store values
Both allows to define methods
Both allows to define subscripts
Both allows to define initializers
Both allows extension and protocols
struct Player {
var name: String
var highScore: Int = 0
var history: [Int] = []
init(_ name: String) {
self.name = name
}
}
var player = Player("Tomas")
struct Car {
var gear = 0
// method inside struct
func applyBrake(){
print("Applying Hydraulic Brakes")
}
}
// create an instance
var car1 = Car()
car1.gear = 5
print("Gear Number: (car1.gear)")
// access method
car1.applyBrake()