Swift Playgrounds Learn to Code 2 "Moving Further Forward" Page
After successfully getting the "expert" to the end of the maze puzzle a box pops up. It talks about defining a function using parameters to make the function more reusable. To make the function work I wrote:
let expert = Expert()
func move(distance: Int) {
for i in 1 ... distance {
expert.moveForward()
}
}
If I try to put moveForward() without expert. as a prefix I get an error. That's OK by me.
However, the 'success box' after completing the maze says: "Now that you've defined move, you'll be able to use is as a method on the Character and Expert types. For example, expert.move(distance: 4) will move the expert instance forward four tiles." I do not see how to define the function so that I can call it this way.
Not knowing the correct words to use in a search on the internet makes it difficult to find an answer.
I tried querying define a function as a method for an instance, or something like that.
Is there a way to write the function so it becomes a method that I can call with either expert.move(distance: 4), or character.move(distance: 4), or even beginner.move(distance: 4)?
MacBook Pro 15″, macOS 10.14