Skip to content

Instantly share code, notes, and snippets.

@Edudjr
Created March 24, 2021 06:08
Show Gist options
  • Save Edudjr/4dce5573eadfae1b1717ae264e76f29d to your computer and use it in GitHub Desktop.
Save Edudjr/4dce5573eadfae1b1717ae264e76f29d to your computer and use it in GitHub Desktop.
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