如何在Swift中将数组的所有成员初始化为相同的值? [英] How to initialize all members of an array to the same value in Swift?
本文介绍了如何在Swift中将数组的所有成员初始化为相同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Swift中有很多阵列.我想将所有成员初始化为相同的值(即它可以为零或其他某个值).最好的方法是什么?
I have a large array in Swift. I want to initialize all members to the same value (i.e. it could be zero or some other value). What would be the best approach?
推荐答案
实际上,使用Swift非常简单.如 Apple的文档,您可以使用相同的重复值初始化数组,如下所示:
Actually, it's quite simple with Swift. As mentioned in the Apple's doc, you can initialize an array with the same repeated value like this:
使用 Swift旧版本:
var threeDoubles = [Double](count: 3, repeatedValue: 0.0)
从 Swift 3.0 开始:
var threeDoubles = [Double](repeating: 0.0, count: 3)
这将给出:
[0.0, 0.0, 0.0]
这篇关于如何在Swift中将数组的所有成员初始化为相同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文