Home > OS >  how i remove key value from dictionary?
how i remove key value from dictionary?

Time:08-30

var student  : [[String:String]] =    [["name"   :  "chirag",
                                       "mobile" : "9925545565",
                                       "email"  :  "[email protected]",
                                       "city"   : "ahmedabad"],
                                      ["name"   :  "ravi",
                                       "mobile" : "6868645565",
                                       "email"  :  "[email protected]",
                                       "city"   : "rajkot"],
                                      ["name"   :  "ajay",
                                       "mobile" : "5788545565",
                                       "email"  :  "[email protected]",
                                       "city"   : "surat"],
                                      ["name"   :  "mukesh",
                                       "mobile" : "575225565",
                                       "email"  :  "[email protected]",
                                       "city"   : "kadi"],
                                      ["name"   :  "avinash",
                                       "mobile" : "7825545565",
                                       "email"  :  "[email protected]",
                                       "city"   : "ahmedabad"]]

CodePudding user response:

For a given dictionary, use the removeValue(forKey:) method.

E.g.

var d = ["hello": "world", "foo": "bar"]
print(d)
// ["hello": "world", "foo": "bar"]
d.removeValue(forKey: "hello")
print(d)
// ["foo": "bar"]

Pair this with a for loop to (for example) remove the "name" key from every element in student.

for var s in student {
  s.removeValue(forKey: "name")
}
  • Related