Kiran Jasvanee Kiran Jasvanee - 2 months ago 200
iOS Question

Swift 3.0: Value of type 'IndexSet' has no member 'enumerateIndexesUsingBlock'

Receiving

Value of type 'IndexSet' has no member 'enumerateIndexesUsingBlock'
error at enumerateIndexesUsingBlock.

/**
Extension for creating index paths from an index set
*/
extension IndexSet {
/**
- parameter section: The section for the created NSIndexPaths
- return: An array with NSIndexPaths
*/
func bs_indexPathsForSection(_ section: Int) -> [IndexPath] {
var indexPaths: [IndexPath] = []

self.enumerateIndexesUsingBlock { (index:Int, _) in
indexPaths.append(IndexPath(item: index, section: section));
}

return indexPaths
}
}

Answer

The Foundation type NSIndexSet has a enumerateIndexesUsingBlock method. The corresponding overlay type IndexSet from Swift 3 is a collection, therefore you can just map each index to an IndexPath:

extension IndexSet {
    func bs_indexPathsForSection(_ section: Int) -> [IndexPath] {
        return self.map { IndexPath(item: $0, section: section) }
    }
}