为什么Dictionary中的"values"属性不是一个简单的数组,而是它的奇怪之处?

BR4*_*FCK 5 arrays macos dictionary ios swift

let dict = [1:"One", 2:"Two", 3:"Three"]
let values = dict.values

print(values.dynamicType)
Run Code Online (Sandbox Code Playgroud)

打印:

LazyMapCollection<Dictionary<Int, String>, String>
Run Code Online (Sandbox Code Playgroud)

这里有两件事我不明白.如果values退回来会不会更简单Array?还有,是什么LazyMapCollection?我查看了Apple的参考资料,但它提供的字面上没有任何信息(或者我没有任何意义).您可以迭代此对象,因为它是CollectionType:

for v in values {
    print(v)
}
Run Code Online (Sandbox Code Playgroud)

打印:

Two
Three
One
Run Code Online (Sandbox Code Playgroud)

但由于某些原因,Apple没有使用Array类型.

aya*_*aio 4

LazyMapCollection是集合的惰性(仅在必要时评估)视图。我所说的“视图”是指“窗口”、“框架”、“虚拟子集”这类概念。

要从中获取实际值,只需使用数组初始值设定项:

let values = dict.values
let result = Array(values)
Run Code Online (Sandbox Code Playgroud)