Jacolack Jacolack - 4 months ago 220
iOS Question

Create NSIndexSet from integer array in Swift

I converted an NSIndexSet to an [Int] array using the answer at http://stackoverflow.com/a/28964059/6481734 I need to do essentially the opposite, turning the same kind of array back into an NSIndexSet.


Swift 3

Because indexset can be created directly from an array literal using init(arrayLiteral:), like so:

let indexes: IndexSet = [1, 2, 3]

Original answer (Swift 2.2)

Similar to pbasdf's answer, but uses forEach(_:)

let array = [1,2,3,4,5,7,8,10]

let indexSet = NSMutableIndexSet()
array.forEach(indexSet.add) //Swift 3
//Swift 2.2: array.forEach{indexSet.addIndex($0)}