我有一个如下字典数组:
{
"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";
}
现在我要删除包含" photo_id的对象:255024731588044"
我该怎么做?
答案 0 :(得分:7)
您可以使用执行.index(where:)
执行的0(n)方法获取要删除的字典的索引:
let index = array.index(where: { dictionary in
guard let value = dictionary["photo_id"] as? Int
else { return false }
return value == 255024731588044
})
如果不是index
:
nil
值将其删除
if let index = index {
array.remove(at: index)
}
在这里你可以download操场。
答案 1 :(得分:1)
假设数组类型为Animal
,您可以使用过滤器删除特定值。尝试这样做
[[String:Any]]
答案 2 :(得分:0)
试试这个:
let searchedPhotoID = 255024731588044
guard let index = arrayOfDict.index(where: {
guard let photoID = $0["photo_id"] as? Int else { return false }
return photoID == searchedPhotoID
}) else {
return // photoID not found - handle somehow
}
arrayOfDict.remove(at: index)