I have a array of dictionary like below:
{
"photo_id" = 255025344921316;
"photo_url" = "https://scontent.xx.fbcdn.net/v/t1.0-0/p320x320/16143181_255025344921316_6580634265541865230_n.jpg?oh=4f6359dbc63241c9dfab29c8cfc570ef&oe=598E85AE";
},
{
"photo_id" = 255024861588031;
"photo_url" = "https://scontent.xx.fbcdn.net/v/t1.0-0/p75x225/16106022_255024861588031_911227627582642933_n.jpg?oh=0bc3fb9df0ccac44c9bd03e8ee1a407d&oe=597A199C";
},
{
"photo_id" = 255024731588044;
"photo_url" = "https://scontent.xx.fbcdn.net/v/t1.0-0/p320x320/16113986_255024731588044_6834401222098779590_n.jpg?oh=68b39d59c803e30754fa6aaab5ea3017&oe=598A3425";
}
Now I want to delete the object that has a "photo_id : 255024731588044"
How can I do it?
You can get the index of the dictionary that you want to delete using .index(where:)
method which performs performs 0(n):
let index = array.index(where: { dictionary in
guard let value = dictionary["photo_id"] as? Int
else { return false }
return value == 255024731588044
})
You can delete it using the index
value if it is not nil
:
if let index = index {
array.remove(at: index)
}
Here you can download the playground.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With