我使用https://stackoverflow.com/a/28964059/6481734的答案将一个Int数组转换为Int数组,实际上我需要做的正好相反,将相同类型的数组转换回一个NSIndexSet。
发布于 2016-06-22 21:05:37
Swift 3
可以使用IndexSet
直接从数组文本创建init(arrayLiteral:)
,如下所示:
let indices: IndexSet = [1, 2, 3]
原文(Swift 2.2)
类似于pbasdf's answer,但使用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)}
print(indexSet)
发布于 2016-06-22 21:08:09
在Swift 3中,这会容易得多:
let array = [1,2,3,4,5,7,8,10]
let indexSet = IndexSet(array)
哇!
发布于 2017-02-07 13:58:39
Swift 3+
let fromRange = IndexSet(0...10)
let fromArray = IndexSet([1, 2, 3, 5, 8])
添加了这个答案,因为还没有提到fromRange
选项。
https://stackoverflow.com/questions/37977404
复制相似问题