I am using a completion handler to sum up numbers. What I don't understand is if I break my code in 2 lines, the number of executions would change from 6 to 7!! WHY?
func summer (from : Int, to: Int, handler: (Int) -> (Int)) -> Int {
var sum = 0
for i in from...to {
sum += handler(i)
}
return sum
}
summer(1, to:6){ //Shows '21'
return $0} // shows '(6 times)'
// Same code, but in 1 line
summer(1, to:6){return $0} // shows '(7 times)'
Fluidity
It's showing how many times a function / expression is being called on that line:
since the calling expression (summer()) is on the same line, it counts as an extra operation. Hence, 6 prints + 6 returns + 1 summer() = 13 times something happened on that line.
I'm not sure if I used the correct terminology, but this is what's going on.
It's merely a consequence of the presentation:
21 //the result from the first time
(6 times) //the other 6 times
(7times) //all 7 times, including the 21 one.
来源:https://stackoverflow.com/questions/39109073/why-does-swift-playground-shows-wrong-number-of-executions