简体   繁体   English

Swift使用var而不是let元素进行循环?

[英]Swift for in loop with var instead of let element?

Is there some kind of for in loop like the following? 是否存在如下所示的for in循环?

for var score in scores {
...
}

I want that score is variable. 我希望分数是可变的。 How can I do that? 我怎样才能做到这一点?

Edit: I want that score is a var instead of a let. 编辑:我希望该分数是var而不是let。 So that I can do: 这样我就可以做到:

score = nil

If you want to assign a variable nil the array must be defined with an optional type. 如果要分配变量nil则必须使用可选类型定义数组。 Perhaps you can use the Array higher order sequence map to construct a new array? 也许您可以使用Array高阶序列图来构建新的数组?

var scores: [Int?] = [1, 2, 3, 4, nil, 5, 6, 7, 8]
scores = scores.map { (score: Int?) -> Int? in
    return nil
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM