Guided Project-Apple Pie: How to use map method in place of loop?

I’m going through Develop in Swift Fundamentals and I’m stuck at a problem where it says “Learn about the map method, and use it in place of the loop that converts the array of characters to an array of strings in updateUI().”

Here’s the code:

 var letters = [String]()

for letter in currentGame.formattedWord {
letters.append(String(letter))
}

Thanks in advance.



var letters = currentGame.formattedWord.map { String($0) }

Another way of writing that is

var letters = currentGame.formattedWord.map { letter in 
return String(letter)
}

We're essentially taking each character ('letter') from formattedWord and initializing a String from that letter using String(letter) and by using the closure on map, we're creating a whole new String array that gets passed into letters. The first solution is just a short-hand syntax for the second one. I recommend looking into closures and array operations.

Guided Project-Apple Pie: How to use map method in place of loop?
 
 
Q