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)
5
  • add an if in your 2nd part Commented May 31, 2016 at 7:45
  • 5
    It depends on whether you want to remove all objects which compare equal to willRemoveObj or only the first (or only) one. Commented May 31, 2016 at 7:47
  • I prefer the second one. Commented May 31, 2016 at 7:48
  • Take a look at @MartinR comment, cause arrays can store duplicated object. Commented May 31, 2016 at 8:18
  • 1
    This might be helpful: stackoverflow.com/a/30724543/1187415. Commented May 31, 2016 at 8:27

1 Answer 1

4

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]

Sign up to request clarification or add additional context in comments.

5 Comments

The function signature mutating func remove(_ object: Element) would make your Swift 3 version adhere to the Swift API design guidelines.
@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)'
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 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.
Hi @IgorB, I see your point. Just for the sake of trying to be helpful: Set's remove element function 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. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.