JKT JKT - 1 year ago 84
Swift Question

Filter an array of integers in-place in swift

One can filter an array like this in swift

var numbers = Array(1...1000000)
numbers = numbers.filter( { return $0 % 2 == 0 } )

How would one go about to filter an array in-place in swift, is it possible to do it in a clean way without writing a for loop?

Answer Source

Any Array is a struct which is a value type. It can't be mutated, only copied or replaced.

Structures and Enumerations Are Value Types

If you want a reference type you'll have to use a class to implement your own type of array which is mutable - or use Foundation's NSMutableArray.