在Swift中从整数数组创建NSIndexSet [英] Create NSIndexSet from integer array in Swift

查看:359
本文介绍了在Swift中从整数数组创建NSIndexSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 https://stackoverflow.com/a/28964059/6481734的答案将NSIndexSet转换为[Int]数组实际上,我需要做相反的事情,将相同类型的数组转换回NSIndexSet.

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.

推荐答案

Swift 3

IndexSet 可以直接使用 init(arrayLiteral:) ,如下所示:

Swift 3

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

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

原始答案(Swift 2.2)

类似于 pbasdf的答案,但使用

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)}

print(indexSet)

这篇关于在Swift中从整数数组创建NSIndexSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆