Will Mays Will Mays - 3 years ago 166
Swift Question

Find String In Dictionary

I have the following dictionary:

[(key: "name", value: G. Snyder), (key: "1", value: 00:01:01), (key: "2", value: 00:02:02)]

I need to search the dictionary and find the index of where the key:"name" exists and delete the pair. How would I go about doing this?

I am struggling because it is a
sortedArray: [(key: String, value: Any)]
, not a regular dictionary like I am used to.


Answer Source

You don't have a dictionary. As you said, you have an array of tuples. Removing an element from an array of tuples is done with filter.

newArray = oldArray.filter { $0.key != "name" }

You can also, as you note "search for a key value of "name" get that index then remove it." (Since you knew this answer, it's unclear what your question is.)

if let index = array.index(where: { $0.key == "name" }) {
    array.remove(at: index)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download