m_power m_power - 8 months ago 27
Swift Question

How to initialize an array in 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?


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:

var threeDoubles = [Double](count: 3, repeatedValue: 0.0)

which would give:

[0.0, 0.0, 0.0]