UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Checkpoint 7: STUCK!

Forums > 100 Days of SwiftUI

I know I'm doing something horribly wrong! Just can't seem to put a finger on it. Need help!

My code for checkpoint 7:

class Animals {
    var legs: Int

    init(legs: Int) {
    self.legs = legs
    }
}

class Dog : Animals {

    func dogSpeak() {
        print("Bark-bark-bark")
    }
}

class Corgi : Dog {
    override func dogSpeak() {
        print("bhawwwwwww")
    }
}

class Poodle : Dog {
    override func dogSpeak() {
        print("Burrrrrrr")
    }
}

class Cat : Animals {
    var isTame: Bool
    init(isTame: Bool, legs:Int) {
    super.init(legs: legs)
        }

    func catSpeak() {
            print("")
    }
}

class Persian : Cat {
    override func catSpeak() {
        print("Meowowoowo")
    }
}

class Lion : Cat {
   override func catSpeak() {
        print("Meeeeeeeeeeo")
    }
}

2      

If you paste your code into Playgrounds and build it, what error code do you get?

One of the skills you start to develop is how to read and interpret the compiler's complaints! Get used to it!

When I pasted your code into Playgrounds, the compiler complained about the Cat class:

Property 'self.isTame' not initialized at super.init call

Looking at your cat class, you'll note that all Cats should have a leg count, and also be identified as being tame or not.

You created an initializer for the Cat class. What exactly are the inital values for each property in a Cat object?

Create a blank cat object on paper. Then work through the steps to initialize a NEW cat object. After you initialized it, WHAT ARE THE VALUES?

Please return here and let us know what you found out!

2      

class Animal {
    var legs: Int

    init(legs: Int) {
        self.legs = legs
    }

}

class Dog: Animal {

    func speak() {
        print("Wuff!")
    }

}

class Corgi: Dog {

    override func speak() {
        print("Woff!")
    }

}

class Poodle: Dog {
    override func speak() {
        print("Wuuudl!")
    }
}

class Cat: Animal {

    var isTame: Bool

    init(legs: Int, isTame: Bool) {
        self.isTame = isTame
        super.init(legs: legs)
    }
    func speak() {
        print("Miau!")
    }
}

class Persian: Cat {

    override func speak() {
        print("Miez!")
    }
}

class Lion: Cat {
    override func speak() {
        print("Roar!")
    }
}

let frodo = Lion(legs: 4, isTame: false)
frodo.speak()

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.