我想从 Swift 5 中的 IndexSet 获取 [IndexPath]。
有一个question将 NSIndexSet 转换为数组 <NSIndexPath *>
在 Objective-C 中。
但是 Swift 5 中没有方法 enumerateIndexesUsingBlock 。
谁认识路?
最佳答案
在索引上使用map
来创建自定义IndexPath
,如下所示:
let customIndexPaths = indexes.map { IndexPath(row: $0, section: 0) }
// Or, You can use the short-form, credits: @Vadian
let customIndexPaths: [IndexPath] = indexes.map { [$0, 0] }
您还可以使用 IndexPath
的初始值设定项:
let indexes: IndexSet = [0, 1]
let indexPath = IndexPath(indexes: indexes) // Note: this initializer returns just one indexPath not an array `[IndexPath]`
关于Swift:将 IndexSet 转换为 [IndexPath],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62328999/