I am using custom code from here to map a Dictionary
to a Dictionary
:
extension Dictionary {
init(_ pairs: [Element]) {
self.init()
for (k, v) in pairs {
self[k] = v
}
}
func mapPairs<OutKey: Hashable, OutValue>(_ transform: (Element) throws -> (OutKey, OutValue)) rethrows -> [OutKey: OutValue] {
return Dictionary<OutKey, OutValue>(try map(transform))
}
func filterPairs(_ includeElement: (Element) throws -> Bool) rethrows -> [Key: Value] {
return Dictionary(try filter(includeElement))
}
}
My Dictionary
looks like this:
[String: (TokenView, MediaModel?)]
and needs to be mapped to [String : MediaModel]
.
Currently this code:
let myDict = strongSelf.tokenViews.mapPairs {($0.key, $0.value.1)}
maps to [String : MediaModel?]
.
What needs to happen is that if the MediaModel
is nil whilst it's being mapped, that key-value pair should not be added to the end Dictionary. How can I modify the code to accommodate for this?