有两种情况让我在使用Xcode 7.1开发swift 2.2时感到困惑,请看下面的例子,谢谢
首先,当导入Foundation时,我声明了一个testArray,它包含两个项目,一个Integer类型1和一个String类型"hello",我的问题是为什么Swift类型推断testArray到Array(NSObject)而不是Array(Any)
import Foundation
let testArray = [1, "hello"]
print(testArray.dynamicType) //testArray is Array<NSObject>
Run Code Online (Sandbox Code Playgroud)
其次,当我删除import Foundation时,下面的代码无法编译,错误信息是"表达式的类型没有更多内容",我的问题是为什么Swift在这种情况下没有对Array(Any)进行类型推断,谢谢求助
let testArray2 = [2, "world"]
print(testArray2)
//can't compile, error message = "Type of expression is ambiguous without more content"
Run Code Online (Sandbox Code Playgroud)