在Swift中删除数组中的重复id

时间:2018-02-04 05:06:25

标签: swift

我有一个带有重复主键和空主键的API。我如何删除那些重复的id,以便我可以将它插入数组并在Swift的tableview中显示?

https://test-api.nevaventures.com/

这是上述链接的输出:

{
  "id": 7,
  "name": "Raghuram Rajan",
  "skills": " Economist",
  "image": "https://teekhapan.files.wordpress.com/2012/08/raghuram-rajan.jpg?w=229&h=300"
},
{
  "skills": "Dancer"
},
{
  "id": 8,
  "name": "Raghuram Rajan",
  "skills": " Economist",
  "image": "https://teekhapan.files.wordpress.com/2012/08/raghuram-rajan.jpg?w=229&h=300"
},
{
  "id": 9,
  "name": "Kumar",
  "skills": "builder",
  "image": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRKJk2hWltaMgJcf3QO6jIXOhmravSvIqEWAl9stEfszcjLNMVzPg"
},
{
  "id": 10,
  "name": "Ranjit",
  "skills": " builder",
  "image": "https://is1-2.housingcdn.com/4f2250e8/ee8bf39d2001fde419f82e0e6e90fb59/v5/_logo/solitare_ranjit_avenue1_bulara-ludhiana-solitare_colonizersand_builders.jpg"
},
{
  "id": 8,
  "name": "Raghuram Rajan",
  "skills": " Economist",
  "image": "https://teekhapan.files.wordpress.com/2012/08/raghuram-rajan.jpg?w=229&h=300"
},
{
  "id": 8,
  "name": "Raghuram Rajan",
  "skills": "Economist",
  "image": "https://teekhapan.files.wordpress.com/2012/08/raghuram-rajan.jpg?w=229&h=300"
},

1 个答案:

答案 0 :(得分:0)

您可以在班级中使用Equatable

class yourClass: Equatable {
    var id :Int = 0
    static func ==(lhs: yourClass, rhs: yourClass) -> Bool {
        return lhs.id == rhs.id
    }
}

然后在将新obj附加到数组之前检查它是否包含obj。