Removing from array during enumeration in Swift?

IosArraysSwiftEnumeration

Ios Problem Overview


I want to enumerate through an array in Swift, and remove certain items. I'm wondering if this is safe to do, and if not, how I'm supposed to achieve this.

Currently, I'd be doing this:

for (index, aString: String) in enumerate(array) {
    //Some of the strings...
    array.removeAtIndex(index)
}

Ios Solutions


Solution 1 - Ios

In Swift 2 this is quite easy using enumerate and reverse.

var a = [1,2,3,4,5,6]
for (i,num) in a.enumerate().reverse() {
    a.removeAtIndex(i)
}
print(a)

Solution 2 - Ios

You might consider filter way:

var theStrings = ["foo", "bar", "zxy"]

// Filter only strings that begins with "b"
theStrings = theStrings.filter { $0.hasPrefix("b") }

The parameter of filter is just a closure that takes an array type instance (in this case String) and returns a Bool. When the result is true it keeps the element, otherwise the element is filtered out.

Solution 3 - Ios

In Swift 3 and 4, this would be:

With numbers, according to Johnston's answer:

var a = [1,2,3,4,5,6]
for (i,num) in a.enumerated().reversed() {
   a.remove(at: i)
}
print(a)

With strings as the OP's question:

var b = ["a", "b", "c", "d", "e", "f"]

for (i,str) in b.enumerated().reversed()
{
    if str == "c"
    {
        b.remove(at: i)
    }
}
print(b)

However, now in Swift 4.2 or later, there is even a better, faster way that was recommended by Apple in WWDC2018:

var c = ["a", "b", "c", "d", "e", "f"]
c.removeAll(where: {$0 == "c"})
print(c)

This new way has several advantages:

  1. It is faster than implementations with filter.
  2. It does away with the need of reversing arrays.
  3. It removes items in-place, and thus it updates the original array instead of allocating and returning a new array.

Solution 4 - Ios

When an element at a certain index is removed from an array, all subsequent elements will have their position (and index) changed, because they shift back by one position.

So the best way is to navigate the array in reverse order - and in this case I suggest using a traditional for loop:

for var index = array.count - 1; index >= 0; --index {
    if condition {
        array.removeAtIndex(index)
    }
}

However in my opinion the best approach is by using the filter method, as described by @perlfly in his answer.

Solution 5 - Ios

No it's not safe to mutate arrays during enumaration, your code will crash.

If you want to delete only a few objects you can use the filter function.

Solution 6 - Ios

Either create a mutable array to store the items to be deleted and then, after the enumeration, remove those items from the original. Or, create a copy of the array (immutable), enumerate that and remove the objects (not by index) from the original while enumerating.

Solution 7 - Ios

The traditional for loop could be replaced with a simple while loop, useful if you also need to perform some other operations on each element prior to removal.

var index = array.count-1
while index >= 0 {

     let element = array[index]
     //any operations on element
     array.remove(at: index)

     index -= 1
}

Solution 8 - Ios

I recommend to set elements to nil during enumeration, and after completing remove all empty elements using arrays filter() method.

Solution 9 - Ios

Just to add, if you have multiple arrays and each element in index N of array A is related to the index N of array B, then you can still use the method reversing the enumerated array (like the past answers). But remember that when accessing and deleting the elements of the other arrays, no need to reverse them.

Like so, (one can copy and paste this on Playground)

var a = ["a", "b", "c", "d"]
var b = [1, 2, 3, 4]
var c = ["!", "@", "#", "$"]

// remove c, 3, #

for (index, ch) in a.enumerated().reversed() {
    print("CH: \(ch). INDEX: \(index) | b: \(b[index]) | c: \(c[index])")
    if ch == "c" {
        a.remove(at: index)
        b.remove(at: index)
        c.remove(at: index)
    }
}

print("-----")
print(a) // ["a", "b", "d"]
print(b) // [1, 2, 4]
print(c) // ["!", "@", "$"]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrewView Question on Stackoverflow
Solution 1 - IosJohnstonView Answer on Stackoverflow
Solution 2 - IosMatteo PiomboView Answer on Stackoverflow
Solution 3 - IosjvarelaView Answer on Stackoverflow
Solution 4 - IosAntonioView Answer on Stackoverflow
Solution 5 - IosStarscreamView Answer on Stackoverflow
Solution 6 - IosWainView Answer on Stackoverflow
Solution 7 - IosLocutusView Answer on Stackoverflow
Solution 8 - IosfreeleView Answer on Stackoverflow
Solution 9 - IosGlenn PosadasView Answer on Stackoverflow