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

One of the places I have noticed this on my MBP is in the 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:

Code Block
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 in Learn to Code 2.

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)?

When searching these forums I don't find a way to refine the search, or search through the results. Putting quote marks around part of the search doesn't search for that phrase. Most annoying having two phrases in quotes results in 403 Forbidden. The forbidden search is:
Swift Playgrounds "Learn to Code 2" "Moving Further Forward" Page
Why would searching for a phrase not work and searching two phrases be forbidden? WOW! 😳

Replies

I stumbled on the exact same question and I'm having the same issues in searching for answers. I'm still learning Swift, but the answers here match my previous programming experience. You can use class inheritance, either inheriting from another class that implements the method or from a protocol (which seems similar to an interface in C#). But the question here is what is the class or protocol that implements the moveForward() function?
It seems Expert and Character derive from the class Actor. So one way to do it is:

Code Block
func mover(persona: Actor, times: Int){
    for i in 1 ... times {
        persona.moveForward()
    }
}

The text when you complete the Moving Further Forward page in Learn to Code 2 is trying to say that, now that you've learned about using parameters in your functions (like being able to specify how many steps forward you want Expert to take), you can use the move(distance:) function on any character. Like you folks were saying above, Expert and Character derive from the same base class, which, if you look into the sources for the playground (on your iPad, in the '...' menu under Advanced > View Auxilliary Source Files) you'll see that the Actor.swift file in the Book.playgroundmodule has a method defined called public func move(distance: Int)

This means that, even before you learned about this method by writing a function with the parameter for distance, there was a method you could call on your expert that would look like this:
Code Block
expert.move(distance: 3)


It didn't show up in the autocompletions until the next page, but it was always there.

Does that make sense?

Probally you already found your answer. but for the next ones who have the same question... Apparently it was only a misunderstanding. You can check this file with official solution codes:

https://education-static.apple.com/teaching-code/learn-to-code-solutions.pdf