为什么 Swift 游乐场显示错误的执行次数? [英] Why does Swift playground shows wrong number of executions?

查看:32
本文介绍了为什么 Swift 游乐场显示错误的执行次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用完成处理程序来总结数字.我不明白的是,如果我将代码分成 2 行,执行次数将从 6 变为 7!!为什么?

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

图片

推荐答案

它显示了在该行调用函数/表达式的次数:

It's showing how many times a function / expression is being called on that line:

由于调用表达式(summer())在同一行,所以算作额外的操作.因此,6 次打印 + 6 次返回 + 1 次 summer() = 13 次该行发生了某些事情.

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.

这篇关于为什么 Swift 游乐场显示错误的执行次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆