Swift: delete all array elements
IosSwiftIos Problem Overview
I am trying to delete all array elements with a for loop like this:
for index 1...myArray.count {
myArray.removeAtIndex(index)
}
But it doesn't work, I get this error before bulding:
> Expected ';' in 'for' statement
Ios Solutions
Solution 1 - Ios
There is a method (actually a function)
myArray.removeAll()
Solution 2 - Ios
Taking for granted that @vadian's answer is the solution, I would just want to point out that your code doesn't work.
First of all, array indexes are 0-based, so you should rewrite the range accordingly:
for index 0..<myArray.count {
myArray.removeAtIndex(index)
}
However this implementation is going to cause a crash. If you have an array of 10 elements, the last element occupies the position at index 9.
Using that loop, at the first iteration the element at index 0 is removed, and that causes the last element to shift down at index 8.
At the next iteration, the element at index 1 is removed, and the last element shifts down at index 7. And so on.
At some point in the loop, an attempt to remove an element for a non existing index will cause the app to crash.
When removing elements from an array in a loop, the best way of doing it is by traversing it in reverse order:
for index in reverse(0..<myArray.count) {
myArray.removeAtIndex(index)
}
That ensures that removed elements don't change the order or the index of the elements still to be processed.
Solution 3 - Ios
If you really want to clear the array, the simplest way is to Re-Initialize it.
Solution 4 - Ios
You're missing the in
keyword which is causing your error. The code should be:
for index in 1...myArray.count {
myArray.removeAtIndex(index)
}
This will however not work as you would expect for a couple of reasons:
- The last valid index is
count - 1
which would require1..<myArray.count
- More importantly removing an element from an array shortens it thus changing the indexes each time.
If you're trying to remove all things from the array then you should do as others have suggested and use:
myArray.removeAll()
In the case that you do only want the first element and nothing else you could get a reference to the first object, empty the array, then add the object back on:
var firstElement = myArray.first!
myArray.removeAll()
myArray.append(firstElement)
Solution 5 - Ios
Your code should work, it is just out of bound.
Swift 3
existingArray = []
By doing this you re-assign an empty array to the existing array and the data type is referred.
Alternatively, you can use removeAll
which removes all elements from the array and provide an option to keep the existing capacity.
existingArray.removeAll()
This is a mutating
function which means the array calling the method will be changed (empty).
Solution 6 - Ios
Kyle is on the right track, but his code will fail as the possible indices reduces during enumerating, leading to illegal indices.
One way to solve it is to enumerate backwards. In swift this is done by using strides.
for index in stride(from: myArray.count - 1, through: 0, by: -1) {
myArray.removeAtIndex(index)
}
another options would be to use filter()
Swift 1.2
myArray = filter(myArray, { (obj) -> Bool in
return false
})
Swift 2
myArray = myArray.filter{ (obj) -> Bool in
return false
}
Solution 7 - Ios
There is removeAllObjects() method available in Swift 2
Syntax : public func removeAllObjects()
Eg.: mainArray.removeAllObjects
To remove element at particular index use :
Syntax : public func removeObjectAtIndex(index: Int)
Eg.: mainArray.removeObjectAtIndex(5)
To remove last element use :
Syntax : public func removeLastObject()
Eg.: mainArray.removeLastObject()
To remove elements in particular range use :
Syntax : public func removeObject(anObject: AnyObject, inRange range: NSRange)
To remove particular element use :
Syntax : public func removeObject(anObject: AnyObject)
Below screenshot shows list of method available in NSMutableArray extension
Solution 8 - Ios
You can also do this:
for _ in myArray
{
myArray.removeAtIndex(0)
}
Solution 9 - Ios
Vadian has the correct answer in terms of what you're trying to accomplish. The issue shown as a result of your code sample is due to the way you're trying to create the for
loop. Index
will start at 0 by default. Adding in a number range after it is not the correct way to create a for loop. That's why you're seeing the error. Instead, create your for loop as follows:
for index in myArray.indices {
// Do stuff
}
Hopefully, this answer will help others that are new to Swift or programming.