随着beta 5的更改,我在以下扩展中遇到与KeyType和ValueType相关的错误。
extension Dictionary {
func filter(predicate: (key: KeyType, value: ValueType) -> Bool) -> Dictionary {
var filteredDictionary = Dictionary()
for (key, value) in self {
if predicate(key: key, value: value) {
filteredDictionary.updateValue(value, forKey: key)
}
}
return filteredDictionary
}
}
我可能会丢失一些内容,但似乎无法在发行说明中找到任何相关更改,并且我知道这在Beta 3中有效。
该Dictionary
声明已更改为只使用Key
和Value
其相关联的类型,而不是KeyType
和ValueType
:
// Swift beta 3:
struct Dictionary<KeyType : Hashable, ValueType> : Collection, DictionaryLiteralConvertible { ... }
// Swift beta 5:
struct Dictionary<Key : Hashable, Value> : CollectionType, DictionaryLiteralConvertible { ... }
因此,您的扩展名只需要是:
extension Dictionary {
func filter(predicate: (key: Key, value: Value) -> Bool) -> Dictionary {
var filteredDictionary = Dictionary()
for (key, value) in self {
if predicate(key: key, value: value) {
filteredDictionary.updateValue(value, forKey: key)
}
}
return filteredDictionary
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句