Some time I need pass external variable to the map() like next code:
let tomap = ["a", "d", "l", "m"]
var currentNumber = 10
let mapped = tomap.map { letter -> String in
var letter = letter
letter += "\(currentNumber)"
currentNumber += 10
return letter
}
currentNumber was captured by map closure. The question is: Is it correct way to use mapping?
Yes, it is a perfectly correct way to use map. Capturing variables is a basic and fundamental part of how closures work, and if that’s what you want to do, they are the right tool for the job.
I will note that you can make your example shorter by using defer: