I have an object in Swift, which is a type dictionary Dictionary<String, String[]>. I would like to be able to filter an array String[]while maintaining a dictionary structure.
let list: Dictionary<String, String[]> = [
"Vegetables" : [ "Carrot", "Potato" ],
"Fruit" : [ "Apple", "Orange", "Banana" ]
]
I would like to be able to filter everything that contains "O", and in the end I get something similar to this:
[
"Vegetables" : [ "Carrot", "Potato" ],
"Fruit" : [ "Orange" ]
]
To filter arrays, I did this:
["Carrot", "Potato"].filter { ($0 as NSString).containsString("o") }
However, the part that I am currently facing is matching through a dictionary, because then I can save the key and call this filter function for the value. How should I do it? Thanks in advance!
source
share