Jac*_*ack 55 nsarray nsindexset ios swift2
我使用/sf/answers/2027484161/上的答案将NSIndexSet转换为[Int]数组我需要完全相反,将相同类型的数组转换回NSIndexSet.
Ale*_*ica 94
IndexSet
可以直接使用数组文字创建init(arrayLiteral:)
,如下所示:
let indices: IndexSet = [1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
类似于pbasdf的答案,但使用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)
Run Code Online (Sandbox Code Playgroud)
mat*_*att 69
在Swift 3中这将更容易:
let array = [1,2,3,4,5,7,8,10]
let indexSet = IndexSet(array)
Run Code Online (Sandbox Code Playgroud)
哇!
Nyc*_*cen 25
Swift 3+
let fromRange = IndexSet(0...10)
let fromArray = IndexSet([1, 2, 3, 5, 8])
Run Code Online (Sandbox Code Playgroud)
添加了此答案,因为fromRange
尚未提及该选项.
小智 5
斯威夫特4.2
从现有阵列中:
let arr = [1, 3, 8]
let indexSet = IndexSet(arr)
Run Code Online (Sandbox Code Playgroud)
从数组文字:
let indexSet: IndexSet = [1, 3, 8]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
33458 次 |
最近记录: |