Using the Undeclared KeyType Type in a Dictionary Extension (Swift)

With changes in beta 5, I am launching an error related to KeyType and ValueType in the extension below.

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 } } 

I might be missing something, but I cannot find any related changes in the release notes, and I know that this worked in beta 3.

+6
source share
1 answer

The Dictionary declaration has changed to use only Key and Value for its associated types instead of KeyType and ValueType :

 // Swift beta 3: struct Dictionary<KeyType : Hashable, ValueType> : Collection, DictionaryLiteralConvertible { ... } // Swift beta 5: struct Dictionary<Key : Hashable, Value> : CollectionType, DictionaryLiteralConvertible { ... } 

So your extension should be:

 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 } } 
+7
source

Source: https://habr.com/ru/post/973392/


All Articles