NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Checkpoint 7 - possible answer

Forums > 100 Days of SwiftUI

I think this works...


//Checkpoint 7

class Animal {
    var numberOfLegs = 4

    init(numberOfLegs: Int) {
        self.numberOfLegs = numberOfLegs
    }
    func speak() {

    }
}

class Dog : Animal {

    override func speak() {

    }

}

final class Corgi : Dog {

    override func speak() {
        print("arf")
    }
}

final class Poodle : Dog {

    override func speak() {
        print("woof")
    }
}

class Cat : Animal {

    var isTame: Bool

    init (numberOfLegs: Int, isTame: Bool) {
        self.isTame = isTame
        super.init(numberOfLegs: numberOfLegs)
    }
    override func speak() {

    }
}

final class Persian : Cat {

    override func speak() {
        print("meooow")
    }
}
final class Lion : Cat {

    override func speak() {
        print("rrooooaaarrr")
    }
}

   

What sound would these animals make?

let puppy = Dog()
puppy.speak()

let kitten = Cat()
kitten.speak()

You should put some kind of default sound in the speak function for Dog and Cat so you don't end up with mute pets. This is what Paul meant when he said Dog.speak() should print "a generic dog barking string". (I assume he wants you to do similar with Cat.speak() but just didn't mention it.)

You have the same issue here:

let pangolin = Animal()
pangolin.speak()

You don't really need a speak method here, I don't think, unless you want to give every other type of animal besides dogs and cats something to say. In which case, you need to fill in the body of the method.

   

Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.