How to make an array of certain size in Swift 4?

Swift is a type-safe language. This essentially means that you can't store a value of some other type (here nil) in a variable/ constant of a particular type (here Float).

So, if you want to store nil values in an array, declare its element type as optional (here Float?).

var computeArray = [Float?](repeating: nil, count:1000)

or

var computeArray = Array<Float?>(repeating: nil, count:1000)

Try this.

var computeArray: Array<Float> = Array(repeating: 0, count: 1000)

or with nils

var computeArray: Array<Float?> = Array(repeating: nil, count: 1000)

Tags:

Arrays

Swift