Checkpoint 7 - Swift for Complete Beginners
HTML-код
- Опубликовано: 23 янв 2025
- Other videos in the Classes section:
1. How to create your own classes: • How to create your own...
2. How to make one class inherit from another: • How to make one class ...
3. How to add initializers for classes: • How to add initializer...
4. How to copy classes: • How to copy classes - ...
5. How to create a deinitializer for a class: • How to create a deinit...
6. How to work with variables inside classes: • How to work with varia...
7. Summary: Classes: • Summary: Classes - Swi...
8. Checkpoint 7: This video
You can find the full set of videos, along with transcriptions, challenges, tests, and more, in my free 100 Days of SwiftUI course: www.hackingwit...
Watch the full Swift for Complete Beginners playlist here: • Swift for Complete Beg...
this man is an absolute legend and this video got 14 likes - for shame. Love your work Paul, thanks!
People are watching these videos on the website, so there is no like button there. You can be sure, there are thousands like the videos.
One way to enforce that subclasses implement the speak() function (until we learn about protocols) is to use assertionFailure() (from the Foundation framework). It won't give you a warning in the code, but if you try to use the speak() of the baseclass, it'll crash:
import Foundation
class Animal {
var legs = 4
init(legs: Int) {
self.legs = legs
}
}
class Dog: Animal {
func speak() {
assertionFailure("Must be overridden")
}
}
class Corgi: Dog {
override func speak() {
print("yip")
}
}
class Poodle: Dog {
// override func speak() {
// print("arf")
// }
}
let c = Corgi(legs: 3)
let p = Poodle(legs: 5)
c.speak() // yip
p.speak() // crash
I'm guessing you meant to say animal class?
love you paul
Remember to set the number of legs for your animals :)
(I forgot at first)
My answer:
class Animal {
var legs: Int
init(_ legs: Int){
self.legs = legs
}
}
class Dog: Animal {
init(legs:Int = 4) {
super.init(legs)
}
func speak() {
print("bark")
}
}
class Corgi: Dog {
var bark = "wauf wauf"
override func speak() {
print(bark)
}
}
class Poodle: Dog {
var bark = "guau guau"
override func speak() {
print(bark)
}
}
class Cat: Animal {
var isTame: Bool
init(tamed isTame: Bool, legs:Int = 4) {
self.isTame = isTame
super.init(legs)
}
func speak() {
print("meow")
}
}
class Persian: Cat {
var meow = "purr"
override func speak() {
print(meow)
}
}
class Lion: Cat {
var meow = "roar"
override func speak() {
print(meow)
}
}
let corgi = Corgi()
print("Corgi (\(corgi.legs) legs)")
corgi.speak()
let poodle = Poodle()
print("Poodle (\(poodle.legs) legs)")
poodle.speak()
var persian = Persian(tamed: true)
print("Persian (\(persian.legs) legs)")
persian.speak()
var lion = Lion(tamed: false)
print("Lion (\(lion.legs) legs)")
lion.speak()
thanks for the code. I am a beginner and it really helped me confirm mine:)
@@kunthshah6140 I’m glad it was helpful :))
Is it possible to to call class Dog with 4 legs and after that, call Poodle without?
Override jus func not init.
let dog = Dog(legs: 4)
dog.speak()
let poodle = Poodle()
poodle.speak()
No
because the classes are inheriting not the instances
@@thepoorsultan5112 Thank You!
@ no problem
import UIKit
class Animal {
var legs: Int
init(legs: Int) {
self.legs = legs
}
}
class Dog: Animal {
func speak () {
var sound = "Bark! Bark!"
print(sound)
}
}
class Corgi: Dog {
override func speak() {
print("Corgi woof!")
}
}
class Poodle: Dog {
override func speak() {
print("Poodle woof!")
}
}
class Cat: Animal {
var isTame: Bool
init(isTame: Bool) {
self.isTame = isTame
super.init(legs: 4)
}
func speak () {
print("Meoww!")
}
}
class Persian: Cat {
override func speak() {
print("Persian meoww!")
}
}
class Lion: Cat {
override func speak() {
print("Lion rawr!")
}
}
let corgi = Corgi(legs: 4)
let poodle = Poodle(legs: 4)
corgi.speak()
poodle.speak()
let persian = Persian(isTame: true)
let lion = Lion(isTame: false)
persian.speak()
lion.speak()
class Animal {
let legs: Int
init(legs: Int) {
self.legs = legs
}
}
class Dog: Animal {
func speak() {
print("hrrrrr rawr")
}
}
class Corgi: Dog {
override func speak() {
print("cute rawr")
}
}
class Kangal: Dog {
override func speak() {
print("hav hav")
}
}
class Cat: Animal {
let isTame: Bool
init(isTame: Bool) {
self.isTame = isTame
super.init(legs: 4)
}
func speak() {
print("original meow sound")
}
}
class Persian: Cat {
override func speak() {
print("meeeeow with too many e letters")
}
}
class Lion: Cat {
override func speak() {
print("meowwwww uWu")
}
}
let corgi = Corgi(legs: 4)
let kangal = Kangal(legs: 4)
corgi.speak()
kangal.speak()
let persian = Persian(isTame: true)
let lion = Lion(isTame: false)
persian.speak()
lion.speak()
thnx man
This is my solution. I also tried to use the deinit functionality and make sure it works properly. (if you want to laugh a bit, use translate for the prints 😅)
import Cocoa
class Animal {
let legs: Int
init(legs: Int) {
self.legs = legs
print("Sunt un animal si am \(legs) picioare!")
}
deinit {
print("Every instance of the class is gone.")
}
}
class Dog: Animal {
func speak() {
print("Ham Ham, sunt un caine!")
}
}
final class Corgi: Dog {
override func speak() {
print("Ham Ham, sunt un caine dragut si am un fund in forma de inima!")
}
}
final class Poodle: Dog {
override func speak() {
print("Ham Ham, sunt un sobolan!")
}
}
class Cat: Animal {
var isTame: Bool
init(legs: Int, isTame: Bool) {
self.isTame = isTame
super.init(legs: legs)
print("Nu sunt salbatica: \(isTame)")
}
func speak() {
print("Miau, Miau, vreau atentie cand vreau!")
}
}
final class Persian: Cat {
override func speak() {
print("Miau, Miau, sunt o mata din Persia!")
}
}
final class Lion: Cat {
override func speak() {
print("Miau, Miau, sunt regele casei!")
}
}
func destroyAfterExecution() {
let animal = Animal(legs: 4)
let caine1 = Dog(legs: 4)
caine1.speak()
let caine2 = Corgi(legs: 4)
caine2.speak()
let caine3 = Poodle(legs: 4)
caine3.speak()
print()
let pisica1 = Cat(legs: 4, isTame: false)
pisica1.speak()
let pisica2 = Persian(legs: 4, isTame: true)
pisica2.speak()
let pisica3 = Lion(legs: 4, isTame: false)
pisica3.speak()
}
destroyAfterExecution()
class Animal {
let legs: Int
init(legs: Int) {
self.legs = legs
}
}
class Dog: Animal {
init() {
super.init(legs: 4)
}
func speak() {
print("Woof")
}
}
class Cat: Animal {
var isTame: Bool
init(isTame: Bool) {
self.isTame = isTame
super.init(legs: 4)
}
func speak() {
print("Meaw")
}
}
class Persian: Cat {
init() {
super.init(isTame: true)
}
override func speak() {
print("Meaaaaaaaaw 😼")
}
}
class Lion: Cat {
init() {
super.init(isTame: false)
}
override func speak() {
print("ROOOOAAAAAAAR 🦁")
}
}
class Corgi: Dog {
override func speak() {
print("Woof woof 🐕")
}
}
class Poodle: Dog {
override func speak() {
print("Woof woof 🐩")
}
}
let poodle = Poodle()
let corgi = Corgi()
let dogo = Dog()
let cat = Cat(isTame: true)
let persi = Persian()
let lowe = Lion()
poodle.speak()
corgi.speak()
dogo.speak()
cat.speak()
persi.speak()
lowe.speak()
starting with animal *class* not struct haha :D 00:25
came here to say this ☝
maby is struct as a structure shortmind but it is same as Swift's keyword :)
class Animal {
var legs: Int
init(legs: Int) {
self.legs = legs
}
}
class Dog: Animal {
override init(legs: Int) {
super.init(legs: legs)
}
func speak() {
print("Woof!")
}
}
class Cat: Animal {
let isTame: Bool
init(legs: Int, isTame: Bool) {
self.isTame = isTame
super.init(legs: legs)
}
func speak() {
print("Meow!")
}
}
class Corgi: Dog {
override init(legs: Int) {
super.init(legs: legs)
}
override func speak() {
print("Bark! I'm a corgi.")
}
}
class Poodle: Dog {
override init(legs: Int) {
super.init(legs: legs)
}
override func speak() {
print("Bark! I'm a poodle.")
}
}
class Persian: Cat {
override init(legs: Int, isTame: Bool) {
super.init(legs: legs, isTame: isTame)
}
override func speak() {
print("Purr! I'm a Persian.")
}
}
class Lion: Cat {
override init(legs: Int, isTame: Bool) {
super.init(legs: legs, isTame: isTame)
}
override func speak() {
print("Roar! I'm a lion.")
}
}
let corgi = Corgi(legs: 4)
corgi.speak() // Bark! I'm a corgi.
let persian = Persian(legs: 4, isTame: true)
persian.speak() // Purr! I'm a Persian.
import Cocoa
class Animal {
let legs = 4
func speak() {
print("All animals make different sounds.")
}
}
class Dog: Animal {
override func speak() {
super.speak()
print("Bow bow bow bow....")
}
}
class Corgi: Dog {
override func speak() {
print("Corgi's bark is bew bew bew")
}
}
class Cat: Animal {
var isTame: Bool
init(isTame: Bool) {
self.isTame = isTame
}
}
class Persian: Cat {
override func speak() {
print("Persian cat speaks mew mew mew...")
}
}
class Lion: Cat {
override func speak() {
super.speak()
print("Lions roar ghrrrr...")
}
}
let anyAnimal = Animal()
print(anyAnimal.legs)
let aDog = Dog()
aDog.speak()
print("A dog has \(aDog.legs) legs.")
let corgi = Corgi()
corgi.speak()
let persian = Persian(isTame: true)
print("Is persian cat tame: \(persian.isTame)")
persian.speak()
let lion = Lion(isTame: false)
print("Are lions tame: \(lion.isTame)")
lion.speak()
//Checkpoint-7
//Class hierarchy of Animal, Dog and Cat as subclasses, Corgi and Poodle as subclasses of Dog, Persian and Lion as subclasses of Cat
class Animal {
var legs: Int = 4
init(legs: Int) {
self.legs = legs
}
}
class Dog: Animal {
func speak() {
print("Baww Baww!!")
}
}
class Cat: Animal {
var isTame: Bool = false
init(isTame: Bool) {
self.isTame = isTame
super.init(legs: 4)
}
func speak() {
print("Meowww")
}
}
class Corgi: Dog {
override func speak() {
print("Bawwwww from Corgi")
}
}
class Poodle: Dog {
override func speak() {
print("Boooo from Poodle")
}
}
class Persian: Cat {
override func speak() {
print("Meeeee (persian cat)")
}
}
class Lion: Cat {
override func speak() {
print("Rooooarrrrrr! from the King! Lion!")
}
}
let MyCat = Lion(isTame: true)
MyCat.speak()
Cat's always speak when they want))
class Animal{
var legs: Int
init(legs: Int) {
self.legs = legs
}
}
class Dog: Animal {
func speak(){
print("Bark! Bark!")
}
}
final class Corgi: Dog {
override func speak() {
print("Bark! Wait for me. Bark!")
}
}
final class Poodle: Dog {
override func speak() {
print("Bark.. I need new haircut, this one is a L.. Bark?..")
}
}
class Cat: Animal {
let isTime: Bool
init(legs: Int, isTime: Bool) {
self.isTime = isTime
super.init(legs: legs)
if isTime {
speak()
}
}
func speak(){
print("Meow...")
}
}
final class Lion: Cat {
override func speak() {
print("Rarrrr! I'm gonna bite you!")
}
}
let Leo = Lion(legs: 4, isTime: true)
let Jackie = Corgi(legs: 4)
Jackie.speak()
import Foundation
import Cocoa
class Animals {
let legs: Int
init(legs: Int) {
self.legs = legs
}
}
class Dog: Animals {
func speak() {
print("Bark Bark")
}
}
class Corgi: Dog {
let name: String
init(name: String)
{
self.name = name
super.init(legs: 4)
}
override func speak() {
print("Bark Bark! My name is \(name), and I am a Corgi.")
}
}
class Poodle: Dog {
let name: String
init(name: String)
{
self.name = name
super.init(legs: 4)
}
override func speak() {
print("Bark Bark! My name is \(name), and I am a Poodle.")
}
}
class Cat: Animals {
let isTame: Bool
init(isTame: Bool) {
self.isTame = isTame
super.init(legs: 4)
}
func speak()
{
print ("Meow")
}
}
class Persian: Cat {
override func speak()
{
print("Type: Persian")
print("Tame: \(isTame)")
print("Meow")
}
}
class Lion: Cat {
override func speak()
{
print("Type: Lion")
print("Tame: \(isTame)")
print("Rawr")
}
}
let dog1 = Corgi(name: "Jackie")
dog1.speak()
let dog2 = Poodle(name: "Max")
dog2.speak()
let cat1 = Persian(isTame: true)
cat1.speak()
let cat2 = Lion(isTame: false)
cat2.speak()
This was one of the easier checkpoitns tbh, hmu if anyone has questions
import UIKit
class Animal {
var legs: Int
init(legs: Int) {
self.legs = legs
}
}
class Dog: Animal {
func speak() {
print("Generic dog barking")
}
}
class Cat: Animal {
let isTame: Bool
init(isTame: Bool) {
self.isTame = isTame
super.init(legs: 4)
}
func speak() {
print("Genetic cat meow")
}
}
class Corgi:Dog {
override func speak() {
print("wangwang1")
}
}
class Poodle:Dog {
override func speak() {
print("wangwang2")
}
}
class Persian: Cat {
override func speak() {
print("meow1")
}
}
class Lion: Cat {
override func speak() {
print("meow2")
}
}
// test the code
let dog1 = Corgi(legs: 4)
dog1.speak()
let cat1 = Persian(isTame: true)
cat1.speak()
print("Is this cat tame? \(cat1.isTame)")
My solution
import Cocoa
class Animal{
var legs:Int
init(legs: Int) {
self.legs = legs
}
}
class Dog:Animal{
override init(legs:Int = 4) {
super.init(legs:legs)
}
func speak(){
print("i am a dog HAv hav hav")
}
}
class Cat:Animal{
var isTame :Bool
func speak(){
print("i am a cat Myau myau")
}
init(isTame: Bool, legs: Int = 4) {
self.isTame = isTame
super.init(legs: legs)
}
}
class Corgi:Dog{
override func speak(){
print("I am a corgi hav hav")
}
}
class Poodle:Dog{
override func speak(){
print("I am a poodle hav hav")
}
}
class Persion:Cat{
override func speak(){
print("I am a persion cat Myau myau")
}
}
class Lion:Cat{
override func speak(){
print("I am a Lion Myau myau")
}
}
Here is my code:
class Animal {
var legs: Int
init (legs: Int) {
self.legs = legs
}
}
class Cat: Animal {
var meow: String
var isTame: Bool
init(speak: String, legs:Int, isTame:Bool) {
self.meow = speak
self.isTame = isTame
super.init(legs: legs)
}
func speak() {
print() }
}
class Dog: Animal {
var bark: String
init(speak: String, legs:Int) {
self.bark = speak
super.init(legs: legs)
}
func speak() {
print()
}
}
class Corgi: Dog {
override func speak () {
print(bark)
}
}
class Poodle: Dog {
override func speak () {
print(bark)
}
}
class Persian: Cat {
override func speak () {
print(meow)
}
}
class Lion: Cat {
override func speak () {
print(meow)
}
}
var mimi = Persian(speak: "Mrr", legs: 4, isTame: true)
mimi.speak()
print (mimi.legs)
print(mimi.isTame)
var momo = Persian(speak: "mr", legs: 4, isTame: true)
momo.speak()
var max = Lion(speak: "Roar", legs: 4, isTame: false)
max.speak()
print (max.legs)
print(max.isTame)
var morpy = Poodle(speak: "bark", legs: 4)
morpy.speak()
print (morpy.legs)
var ein = Corgi(speak: "BARK", legs: 4)
ein.speak()
print(ein.legs)
My solution:
import Cocoa
class Animals {
var legs: Int
init(legs: Int) {
self.legs = legs
}
}
class Dog: Animals {
func speak() { print("Dog Noise") }
override init(legs: Int) {
super.init(legs: legs)
}
}
class Corgi: Dog {
override func speak() { print("Corgi bark") }
override init(legs: Int) {
super.init(legs: legs)
}
}
class Poodle: Dog {
override func speak() { print("Poodle bark") }
override init(legs: Int) {
super.init(legs: legs)
}
}
class Cat: Animals {
var isTame: Bool
func speak() { print("Cat Noise") }
init(legs: Int, isTame: Bool) {
self.isTame = isTame
super.init(legs: legs)
}
}
class Persian: Cat {
override func speak() { print("Persian hiss") }
override init(legs: Int, isTame: Bool) {
super.init(legs: legs, isTame: isTame)
}
}
class Lion: Cat {
override func speak() { print("Lion hiss") }
override init(legs: Int, isTame: Bool) {
super.init(legs: legs, isTame: isTame)
}
}
let corgi = Corgi.init(legs: 4)
corgi.speak()
let poodle = Poåodle.init(legs: 4)
poodle.speak()
print()
let persian = Persian.init(legs: 4, isTame: true)
persian.speak()
let lion = Lion.init(legs: 4, isTame: false)
lion.speak()
// 3 hours of work xD