Swift 中的字典不符合ExtensibleCollectionType
. 因为它很容易扩展(它在某种程度上不适用于 Swift 1.2;使用 Swift 2):
extension Dictionary: ExtensibleCollectionType {
// ignoring this function
mutating public func reserveCapacity(n: Int) {}
mutating public func append(x: Dictionary.Generator.Element) {
self[x.0] = x.1
}
mutating public func extend<S : SequenceType where S.Generator.Element == Dictionary.Generator.Element>(newElements: S) {
for x in newElements {
self.append(x)
}
}
}
如果这样做,还可以添加字典(另请参阅:添加序列类型)
在标准库中不实现这个有什么好处吗?