Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing from array during enumeration in Swift?

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) } 
like image 898
Andrew Avatar asked Feb 04 '15 14:02

Andrew


1 Answers

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) 
like image 149
Johnston Avatar answered Sep 28 '22 15:09

Johnston