为什么 "testScores["dave"]" 的类型是 "Optional<Array<Int>>.Type" 而不是 "<Array<Int>>.Type"
Why the type of "testScores["dave"]" is "Optional<Array<Int>>.Type" instead of "<Array<Int>>.Type"
我是 Swift 的新手,正在尝试学习下标的概念。当我试图找出 testScores["dave"] 的返回值时,我期待 Array<Int>.Type
,然而,IDE 却输出了 Optional<Array<Int>>.Type
.
为什么会这样?我错过了什么吗?
var testScores = ["dave": [82, 84, 86], "jen": [23, 14, 5], "ben": []]
testScores["dave"].dynamicType
任何时候从 Dictionary
中获取值都是可选的。它可能有也可能没有指定键的条目。这就是为什么你必须在使用它之前打开可选的。
我是 Swift 的新手,正在尝试学习下标的概念。当我试图找出 testScores["dave"] 的返回值时,我期待 Array<Int>.Type
,然而,IDE 却输出了 Optional<Array<Int>>.Type
.
为什么会这样?我错过了什么吗?
var testScores = ["dave": [82, 84, 86], "jen": [23, 14, 5], "ben": []]
testScores["dave"].dynamicType
任何时候从 Dictionary
中获取值都是可选的。它可能有也可能没有指定键的条目。这就是为什么你必须在使用它之前打开可选的。