How to remove the keys of a dictionary where the values are null in swift 3 -
hi need remove dictionary keys having no values .i tried many codes not working .i need dictionary keys have values , need in swift 3
["h": [], "x": [], "d": ["d1\r\n\r\n"], "j": ["j1\r\n"], "i": [], "m": [], "z": [], "s": [], "a": ["a2"], "c": ["c2\r\n\r\n"], "n": [], "y": [], "r": [], "g": [], "e": ["emmm\r\n"], "v": [], "u": [], "l": [], "b": ["b2"], "k": ["king"], "f": [], "o": [], "w": [], "t": ["test", "test2"], "p": [], "q": ["queen"]]
i think meant "remove kvps have []
values", not "remove kvps have nil
values".
a call filter
work, returns array of tuples of kvps gotta add them new dictionary using loop.
let dict = ["h": [], "x": [], "d": ["d1\r\n\r\n"], "j": ["j1\r\n"], "i": [], "m": [], "z": [], "s": [], "a": ["a2"], "c": ["c2\r\n\r\n"], "n": [], "y": [], "r": [], "g": [], "e": ["emmm\r\n"], "v": [], "u": [], "l": [], "b": ["b2"], "k": ["king"], "f": [], "o": [], "w": [], "t": ["test", "test2"], "p": [], "q": ["queen"]] var newdict = [string: [string]]() (key, value) in dict.filter({ !$0.1.isempty }) { newdict[key] = value }
alternatively, can looping through dictionary once:
let dict = ["h": [], "x": [], "d": ["d1\r\n\r\n"], "j": ["j1\r\n"], "i": [], "m": [], "z": [], "s": [], "a": ["a2"], "c": ["c2\r\n\r\n"], "n": [], "y": [], "r": [], "g": [], "e": ["emmm\r\n"], "v": [], "u": [], "l": [], "b": ["b2"], "k": ["king"], "f": [], "o": [], "w": [], "t": ["test", "test2"], "p": [], "q": ["queen"]] var newdict = [string: [string]]() (key, value) in dict !value.isempty { newdict[key] = value }
wiki
Comments
Post a Comment