I have this function:
func flatten(dict: Dictionary>) -> Dictionary {
v
You can do it in a simpler way. This works with Swift 4:
extension Dictionary {
func flatten() -> [Key: Wrapped] where Value == Optional {
return filter { $1 != nil }.mapValues { $0! }
}
}
If you don't like the use of higher order functions or need compatibility with previous versions of Swift you can do this as well:
extension Dictionary {
func flatten() -> [Key: Wrapped] where Value == Optional {
var result: [Key: Wrapped] = [:]
for (key, value) in self {
guard let value = value else { continue }
result[key] = value
}
return result
}
}