Skip to content

Instantly share code, notes, and snippets.

@neonichu
Created December 30, 2014 21:33
Show Gist options
  • Save neonichu/fe11ef46b7d5acd75bd7 to your computer and use it in GitHub Desktop.
Save neonichu/fe11ef46b7d5acd75bd7 to your computer and use it in GitHub Desktop.
Today's Swift WTF - Arrays
#!/usr/bin/env xcrun swift
func info<T>(x: T) {
println("\(x) is a \(_stdlib_getDemangledTypeName(x))")
}
let array = [0, 1, 2] // appending 'as AnyObject' here yields a compiler error
info(array)
import Foundation
let objc_array: AnyObject = [0, 1, 2] as AnyObject
info(objc_array)
// comparing different array types => compiler error as well
//let equal = objc_array == array
@neonichu
Copy link
Author

Output:

[0, 1, 2] is a Swift.Array
(
    0,
    1,
    2
) is a Swift._NSSwiftArrayImpl

or in other words, ObjC compatibility of arrays is achieved by having two implementations, chosen by the context they are used in and by the presence of a Foundation import.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment