在Dictionary扩展(Swift)中使用未声明的类型'KeyType'

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中有效.

Nat*_*ook 7

Dictionary声明已更改为只使用KeyValue其相关联的类型,而不是KeyTypeValueType:

// 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)