How to initialize all members of an array to the same value in Swift?

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:

With old Swift version:

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

Since Swift 3.0:

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

which would give:

[0.0, 0.0, 0.0]

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)