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

Posted on Jul 25, 2020 4:48 PM

Reply
1 reply

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Swift Playgrounds Learn to Code 2 "Moving Further Forward" Page

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.