Krutarth Patel Krutarth Patel - 29 days ago 19
iOS Question

how to delete group data in swift

RightNow i have Dictionary.and i am adding to tableView.

let myArray = [["name": "First element", "foo": "bar","GroupID":"1"], ["name": "First element", "foo": "bar","GroupID":"1"],["name": "Second element", "foo": "bar","GroupID":"2"], ["name": "Second element", "foo": "bar","GroupID":"2"]]


in this dictionary i have key GroupID.

is it possible that i can get dictionary data whose GroupID = %@

i want to filter data based on grouping

suppose i have four data. and group id is same in two data. i want to delete all data whose GroupID is same from dictionary

if groupid == 1 than data will be deleted from dictionary whose GroupID == 1 and tableview will reload data.

i am doing this because i want to delete group data.
can you please help me?

Answer

You can use filter on your Array to remove those dictionaries from your array, check this example

var subitems: [[String:String]] = [
    ["groupid": "1", "b": "2"],
    ["groupid": "3", "b": "4"],
    ["groupid": "1", "b": "6"]
]

var filteredSubitems = subitems.filter{
    $0["groupid"] != "1" // change with your groupid
}

print(filteredSubitems)