1

i am encountering weird error messages i cannot make sense of when declaring arrays with nil declare values.

I am doing this in Xcode playground.

//this is ok
var threeDoubles = [Double](count: 3, repeatedValue: 0.0)

//error: incorrect argument label in call, have count expected "arrayLiteral"
var threeDoubles2 = [Double](count: 3)

//error: extra argument "repeatedValue" in call
var threeDoubles2 = [Double](count: 3, repeatedValue: nil)

1 Answer 1

3

Ok, I found my own bug. Basically, the values cannot be nil because I declared the array as Double, and not Double?

It has nothing to do with an extra argument, which was what xcode was showing me.

var threeDoubles2 = [Double?](count: 3, repeatedValue: nil)

The arcane error messages can really throw you off course even for simple errors.

Sign up to request clarification or add additional context in comments.

1 Comment

Using the [Type]() syntax for initialization when you’re supplying parameters looks kind of odd to me tbh. You could also try var threeDoubles2 = Array(count: 3, repeatedValue: nil as Double?) or var threeDoubles3: [Double?] = Array(count: 3, repeatedValue: nil) or var threeDoubles3 = Array<Double?>(count: 3, repeatedValue: nil).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.