mic*_*eeb 6 swift xcode6 swift-extensions
随着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
}
}
Run Code Online (Sandbox Code Playgroud)
我可能会遗漏一些东西,但我似乎无法在发行说明中找到任何相关的更改,我知道这在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 { ... }
Run Code Online (Sandbox Code Playgroud)
所以你的扩展只需要:
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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2564 次 |
| 最近记录: |