Swift:将 IndexSet 转换为 [IndexPath]

标签 swift indexpath

我想从 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/

相关文章:

ios - 自定义单元格类中的 Indexpath.row

ios - 在 Swift 中获取 UIcollectionView 中的下一个单元格

swift - 如何将数组传递给 Swift 中可变参数的函数?

ios - 为什么我的 UITextView 中的字体会无缘无故地快速更改样式?

ios - Swift:从 GameViewController 设置 GameScene 变量

swift - Swift 中 dispatch_once 的例子

swift - 从子 Collection View 中检索所有值

swift - Swift 3 : Cannot convert value of type 'NSMutableDictionary' to expected argument type '[AnyHashable : Any]!'

ios - 通过自定义 UITableViewCell 上的 UISwipeGestureRecognizer 更新 CoreData?

ios - 如何检查 IndexPath 是否有效?