2

I am just getting started with swift and I know we have a default array initialiser in swift and the syntax goes like this:

    let myArray = [Int](count:3 , repeatedValue:2)//int types [2,2,2]

But when I remove [Int] from the statement,it initialises the array with values (3,2).

    let myArray = (count:3 , repeatedValue:2)//[3,2]

Can anyone explain this behaviour?

1 Answer 1

9

In the second example, you're getting a tuple, not an Array. If you don't want to specify [Int], you still need to specify Array, like this:

let myArray = Array(count: 3, repeatedValue: 2)

Learn more about tuples in the Swift book.

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

Comments

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.