arrayOfTuples = [(4, 4, "id1"), (3, 6, "id2"), (3, 6, "id3")]
How to remove the item with the id2
string?
You can use RangeReplaceableCollection
method removeAll(where:)
and pass a predicate:
var arrayOfTuples = [(4, 4, "id1"), (3, 6, "id2"), (3, 6, "id3")]
arrayOfTuples.removeAll(where: {$2 == "id2"})
print(arrayOfTuples) // [(4, 4, "id1"), (3, 6, "id3")]
If you would like to remove only the first occurrence where the third element of your tuple is equal to "id2" you can use Collection
's method firstIndex(where:)
:
if let index = arrayOfTuples.firstIndex(where: {$2 == "id2"}) {
arrayOfTuples.remove(at: index)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.