Categories
SwiftUI

SwiftUI Day 10 of 100 – Classes

Classes and structs are different, but confusingly similar. With context and practical examples, I’m sure I’ll see more of a difference, but at this point, I’m not entirely sure when I’d use one vs the other.

I’m still very much itching for practical uses of this stuff – I’m getting pretty tired of writing code like this:

class Dog {
    var name: String
    var breed: String
    init(name: String, breed: String) {
        self.name = name
        self.breed = breed
    }
    
    func makeNoise() {
        print("Wooooof")
    }
}

class Pug : Dog {
    init(name: String) {
        super.init(name: name, breed: "Pug")
    }
    
    override func makeNoise() {
        print("Bark, bark, bark!")
    }
}

let oliver = Pug(name: "Oliver")
oliver.makeNoise()

References

Leave a Reply

Your email address will not be published. Required fields are marked *