是for循环条件评估每个循环在迅速? [英] Is the for loop condition evalutaed each loop in swift?

查看:155
本文介绍了是for循环条件评估每个循环在迅速?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在工作中有一个小小的争论:在运行它的项目之前,快速计算Array的大小是否是一个好习惯?这意味着什么是更好的代码练习:

选项A:



<$ p $对于(var i = 0; i< mKeyboardTypesArray.count; i ++){
self.mKeyboardTypesArray [i] = false
,func setAllToFalse(){
} b
$ b

选项B:


$ b $ pre $ func setAllToFalse(){
let typesCount = mKeyboardTypesArray.count
for(var i = 0; i< typesCount ; i ++){
self.mKeyboardTypesArray [i] = false
}
}

当然,如果我在循环期间不改变数组。



是的,我查阅了文档说明:



lockquote

循环执行如下:

首次输入循环时,初始化表达式是
评估一次,以设置任何常量或变量循环需要
。条件表达式被评估。如果将
赋值为false,则循环结束,代码将继续执行for
循环的右大括号(})。如果表达式的计算结果为真,代码
的执行继续执行大括号内的语句。
执行完所有语句后,增量表达式为
求值。它可能会增加或减少计数器的值,或
根据语句的
结果将其中一个初始化变量设置为新值。在增量表达式被赋值
后,执行返回到步骤2,并且再次计算条件表达式


解决方案

在Swift中这样说的这种惯用方法是:

  func setAllToFalse(){
mKeyboardTypesArray = mKeyboardTypesArray.map {_ in false}
}


这样,就没有什么值得评估的东西了。



事实上,这将是一个很好的数组方法:

$ p $ {code $}扩展数组{
mutating func setAllTo(newValue:T){
self = self.map {_ in newValue}
}
}

现在你可以说:
$ b $ pre $ mKeyboardTypesArray.setAllTo(false)

或者,你可以这样做(这涉及到 count ,但只有一次):

  mKeyboardTypesArray = Array(count:mKeyboardTypesArray.count,repeatedValue:false)


I have a small debate at work: Is it a good practice to calculate the size of the Array in swift before running over it's items? meaning what would be a better code practice:

Option A:

    func setAllToFalse() {
        for (var i = 0; i < mKeyboardTypesArray.count; i++ ) {
            self.mKeyboardTypesArray[i] = false
        }
    }

or Option B:

    func setAllToFalse() {
        let typesCount = mKeyboardTypesArray.count
        for (var i = 0; i < typesCount; i++ ) {
            self.mKeyboardTypesArray[i] = false
        }
    }

All of course if I don't alter the Array during the loops.

And yes I went over the documentation which states this:

The loop is executed as follows:

When the loop is first entered, the initialization expression is evaluated once, to set up any constants or variables that are needed for the loop. The condition expression is evaluated. If it evaluates to false, the loop ends, and code execution continues after the for loop’s closing brace (}). If the expression evaluates to true, code execution continues by executing the statements inside the braces. After all statements are executed, the increment expression is evaluated. It might increase or decrease the value of a counter, or set one of the initialized variables to a new value based on the outcome of the statements. After the increment expression has been evaluated, execution returns to step 2, and the condition expression is evaluated again.

Thanks in advance.

解决方案

The idiomatic way to say this in Swift is:

func setAllToFalse() {
    mKeyboardTypesArray = mKeyboardTypesArray.map {_ in false}
}

That way, there is nothing to evaluate and nothing to count.

In fact, this would make a nice Array method:

extension Array {
    mutating func setAllTo(newValue:T) {
        self = self.map {_ in newValue}
    }
}

Now you can just say:

mKeyboardTypesArray.setAllTo(false)

Alternatively, you could do it this way (this involves taking the count, but only once):

mKeyboardTypesArray = Array(count:mKeyboardTypesArray.count, repeatedValue:false)

这篇关于是for循环条件评估每个循环在迅速?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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