Classes are similar to structs, but there are still some differences

Creating Classes

An initializer is always required if you have properties in your class

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Student {
    var name: String
    var age: Int
    init(name: String, age: Int) {
        self.name = name
        self.age = age
    }
}

let bill = Student(name: "Bill", age: 50)

Class Inheritance

Classes in Swift can inheritance from parent class. Child class will inherit all properties and methods from parent, but still can have its own initializer

Parent Class

1
2
3
4
5
6
7
8
9
class Car {
    var name: String
    init(name: String) {
        self.name = name
    }
    func horn() {
        print("Brmmmm....")
    }
}

Child Class

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Tesla {
    init() {
        self.name = "Tesla"
    }
}

var tesla = Tesla()
print(tesla.name)
// Tesla
tesla.horn()
// Brmmmm....

Overriding Methods

Child classes can replace parent methods with the override keyword

Parent Class

1
2
3
4
5
6
7
8
9
class Car {
    var name: String
    init(name: String) {
        super.init(name: "Tesla")
    }
    func horn() {
        print("Brmmmm....")
    }
}

Child Class

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Tesla: Car {
    init() {
        super.init(name: Tesla)
    }
    override func horn() {
        print("Hey...Hey...Brmmmm....")
    }
}

var honda = Car(name: "Honda")
honda.horn()
// Brmmmm....

var tesla = Tesla()
tesla.horn()
// Hey...Hey...Brmmmm....

Final Classes

We can make other classes can not inherit from our class, using the final keyword

1
2
3
4
5
6
final class Sport {
    var name: String
    init(name: String) {
        self.name = name
    }
}

Copying Class Objects

When we copy a struct, the original and the copy are different things in the memory. But when a class object is copied, what we get is a reference - the original object and the copy are the same thing, with same place in the memory. So if we modify the copy, the original object will be modified

Struct Copying

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
struct Movie {
    var name: String
    var rating: Int
}

var starWars = Movie(name: "Star Wars", rating: 5)

var copy = startWars
copy.rating = 4

print(copy.rating)
// 4
print(starWars.rating)
// 5

Class Copying

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Movie {
    var name: String
    var rating; Int
    init(name: String, rating: String) {
        self.name = name
        self.rating = rating
    }
}

var starWars = Movie(name: "Star Wars", rating: 5)

var copy = starWars
copy.rating = 4

print(copy.rating)
// 4
print(starWars.rating)
// 5

Class Deinitializer

We can specify what to do when a class object is destroyed

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Movie {
    var name: String

    init(name: String) {
        self.name = name
    }
    
    deinit {
        print("\(name) has ben destroyed")
    }
}

for _ in 1...3 {
    let movie = Movie(name: "Some movie")
}
// some movie
// some movie has been destroyed
// some movie
// some movie has been destroyed
// some movie
// some movie has been destroyed

Mutability

Class’s methods can always modify its properties

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Sport {
    var name: String
    var isOlympic: Bool
    init(name: String) {
        self.name = name
        isOlympic = false
    }
    func makeOlympic() {
        isOlympic = true
    }
}

var badminton = Sport(name: "Badminton")
badminton.makeOlympic()
print(badminton.isOlympic)
// true