David Grandinetti

Removing Items from a Swift Array

Aug 23, 2014

While the semantics of Swift are still being ironed out, it seems like there is a bit of discussion about what that may look like, specifically when it comes to Arrays.

Here’s a simple Array extension to remove instances of a specific item. The type details were inspired by ExSwift, but I didnt’ want to create a new array like pNre did.

extension Array {
  mutating func remove <U: Equatable> (object: U) {
    for i in stride(from: self.count-1, through: 0, by: -1) {
      if let element = self[i] as? U {
        if element == object {
          self.removeAtIndex(i)
        }
      }
    }
  }
}

var a = [1,2,3,2,4]
println(a) // "[1,2,3,2,4]"
a.remove(2)
println(a) // "[1,3,4]"

Update from @orta: The other part of this that can be tricky is making sure your objects are properly equatable.

class TinkerObject : Equatable {
  // your class impl...
}

// note that this is OUTSIDE of your class impl to make it global
public func ==(lhs: TinkerObject, rhs: TinkerObject) -> Bool {
  return lhs.id == rhs.id
}