4

What is the better way to remove an object from an Array in Swift?

var testArray:[SomeObject]
let willRemoveObj:SomeObject
...

testArray = testArray.filter({ $0 != willRemoveObj })

or

guard let index = testArray.indexOf(willRemoveObj) else {
  return
}
testArray.removeAtIndex(index)
Sulthan
  • 128,090
  • 22
  • 218
  • 270
Kein Sung
  • 51
  • 1
  • 7

1 Answers1

5

There is no convenient method in the Foundation. But we can extend Array structure with needed one. Consider this:

extension Array where Element: Equatable {

    mutating func remove(object: Element) {

        if let index = index(of: object) {

            remove(at: index)
        }
    }
}

var testArray: Array<Int> = [1, 2]
let toBeRemoved: Int = 1

testArray.remove(object: toBeRemoved)

testArray

Results in

[2]

Igor B.
  • 2,219
  • 13
  • 17
  • 1
    The function signature `mutating func remove(_ object: Element)` would make your Swift 3 version adhere to the [Swift API design guidelines](https://swift.org/documentation/api-design-guidelines/#argument-labels). – 0x5f3759df Mar 19 '17 at 08:14
  • @0x5f3759df I have updated the answer. I think that "object" label is needed as it clarifies method semantics. What if we have array of indexes and want to remove someone. Feel the difference between: 'remove(someIndex)' and 'remove(at: index)' – Igor B. Mar 19 '17 at 11:25
  • Hey @Igor B, thanks for your comment! I could be very wrong because I am not a native speaker and a total Swift noob, but as far as I understand the _Swift API design guidelines_ I would think that the "_When the first argument forms part of a prepositional phrase, give it an argument label._" rule applies to the `func remove(at index: Int) -> Element` function and the "_Otherwise, if the first argument forms part of a grammatical phrase, omit its label_" rule applies to `mutating func remove(_ object: Element)` function. :) – 0x5f3759df Mar 19 '17 at 17:23
  • @0x5f3759df Sorry for late response. Your English is fine. BTW I am not a native speaker too :) Your understanding of Swift API design guidelines the same with mine. "Object" label is not necessary according to it. But as there is another removing method, which does item deletion at a particular index I prefere to keep "object" label for parameter. In case of array of indexes, we can get wired bugs due to possible confusion. I think that the case to break the rule. Actually, from my perspective, the guideline is just a set of recomendations which we can ignore if needed. – Igor B. Mar 22 '17 at 13:13
  • Hi @IgorB, I see your point. Just for the sake of trying to be helpful: [Set's remove element function](https://developer.apple.com/reference/swift/set/1540257-remove) `mutating func remove(_ member: Element)` function also follows the "_Swift API design guidelines_" in the way that I described. The difference between `remove(at index: Int)` and `remove(_ member: Element)` is that with the former removes the element _at_ the index and the latter removes the member. That all said, I agree that you perspective should be more important than mine! Thanks for your time, appreciate it. :) – 0x5f3759df Mar 22 '17 at 18:21