Search code examples
iosarraysswiftswift2nsindexpath

finding intersection of 2 NSIndexPath arrays


I'm trying to find which NSIndexPaths are both visible and selected at the same time. The two arrays are initialised like this:

let visibleSelectedItems = collectionView.indexPathsForVisibleItems()

let selectedItems = collectionView.indexPathsForSelectedItems()

I am basically looking for intersection of 2 arrays. Index path is declared as NSIndexPath(forItem: Int, inSection: Int)


Solution

  • Exactly as you said - take the intersection. You can do this easily by coercing both arrays to Sets. A Set has an intersection method. You can then coerce back to an array if you need to.