Array extension to remove object by value

前端 未结 15 1212
我在风中等你
我在风中等你 2020-11-22 08:30
extension Array {
    func removeObject(object: T) {
        var index = find(self, object)
        self.removeAtIndex(index)
    }
}
         


        
15条回答
  •  执笔经年
    2020-11-22 08:36

    I managed to remove a [String:AnyObject] from an array [[String:AnyObject]] by implementing a count outside of a for loop to represent the index since .find and .filter are not compatible with [String:AnyObject].

    let additionValue = productHarvestChoices[trueIndex]["name"] as! String
    var count = 0
    for productHarvestChoice in productHarvestChoices {
      if productHarvestChoice["name"] as! String == additionValue {
        productHarvestChoices.removeAtIndex(count)
      }
      count = count + 1
    }
    

提交回复
热议问题