Created
March 24, 2021 06:08
-
-
Save Edudjr/4dce5573eadfae1b1717ae264e76f29d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct AnimalReport { | |
var animal: Animal? | |
func describe() { | |
guard let animal = animal else { return } | |
print("Animal is called \(animal.name) and is \(animal.age) years old") | |
} | |
} | |
struct AnimalSelection { | |
let animals: [Animal] | |
var animalReport: AnimalReport | |
mutating func selectAnimal(_ animal: Animal) { | |
animalReport.animal = animal | |
animalReport.describe() | |
} | |
} | |
let animals: [Animal] = [ | |
Dog(name: "Doggy", age: 5), | |
Horse(name: "Horsy", age: 7) | |
] | |
let b = AnimalReport() | |
var a = AnimalSelection(animals: animals, animalReport: b) | |
// Runtime | |
a.selectAnimal(animals[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment