1

From Swift Programming Guide:

You can create an empty array of a certain type using initializer syntax:

var someInts = [Int]()
println("someInts is of type [Int] with \(someInts.count) items.")
// prints "someInts is of type [Int] with 0 items."

Note that the type of the someInts variable is inferred to be [Int] from the type of the initializer.

But if you actually copy this as is into XCode, and check the type of someInts, you get [(Int)], which is an array of single element Int tuples. Why this discrepancy? You can also initialize an array as follows:

var someInts: [Int] = []

That has the correct type.

Practically these types seem to behave the same, but I'm trying to figure out what's going on here.

Also in the reference doc for Array it says:

Creating an array using this initializer:

var emptyArray = Array<Int>() 

is equivalent to using the convenience syntax:

var equivalentEmptyArray = [Int]()

But emptyArray above has the type Array<Int> while equivalentEmptyArray has type [(Int)] as in the previous example. Array<Int> is just the full form of [Int] though, so this isn't really a problem, except that the latter doesn't match the type the doc says it is.

Note: finding types by option-clicking on the variable or by selecting it and looking in the Quick Help Inspector in the right side menu.

1 Answer 1

1

You are right, [Int] and [(Int)] actually behave exactly the same (in fact they are the same).

Unfortunately I don't know the exact reason why this is happening but as of Xcode 7 beta 5 (and probably earlier versions) it only shows the type as [Int].

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.