I was trying out Swift arrays in the Playground, when I noticed this peculiar behaviour
The below code works fine.
import UIKit
var array = [1,2,3,"Booyaa"]
But, as soon as I remove the "import UIKit" line from the Playground, I get the following error
Playground execution failed: /var/folders/tx/tvyf1r314wj9371f491qx8wjbqbgsr/T/./lldb/11708/playground71.swift:2:17: error: 'Int' is not convertible to 'IntegerLiteralConvertible' var array = [1, 2, 3, "Booyaa"]
Why does this happen?
The correct answer is here, in response to an identical question I posted on the Apple Dev forums => https://forums.developer.apple.com/message/35389
It's because Swift Arrays can only contain objects that are the same type. Your array has integers and a string. When you import UIKit the objects in the array become objects of type NSObject by virtue of Swift's inference engine. To see for yourself, add the line after the array definition, you'll see the type as Swift.array
import UIKit
var array = [1,2,3,"Swift2"]
array.dynamicType