I converted an NSIndexSet to an [Int] array using the answer at https://stackoverflow.com/a/28964059/6481734 I need to do essentially the opposite, turning the same kind of array back into an NSIndexSet.
Create NSIndexSet from integer array in Swift
48.3k Views Asked by Jacolack At
5
There are 5 best solutions below
0
On
You can use a NSMutableIndexSet and its addIndex method:
let array : [Int] = [1,2,3,4,5,7,8,10]
print(array)
let indexSet = NSMutableIndexSet()
for index in array {
indexSet.addIndex(index)
}
print(indexSet)
3
On
This will be a lot easier in Swift 3:
let array = [1,2,3,4,5,7,8,10]
let indexSet = IndexSet(array)
Wow!
Swift 3
IndexSetcan be created directly from an array literal usinginit(arrayLiteral:), like so:Original answer (Swift 2.2)
Similar to pbasdf's answer, but uses
forEach(_:)