IndexPath.row V.S. IndexPath.item?

Hi Developers,


I am curious the difference between IndexPath.row and IndexPath.item. In my experiment result of iOS code using table view, these two properties had identical values at all the time. After some search I found this page telling they are actually the functionally the same, while people usually use "row" for table view and "item" for collection view for style convention.

https://stackoverflow.com/questions/14765730/nsindexpath-item-vs-nsindexpath-row


I want to double check: is this true? are they really functionally identical? if identical what made them appear in the API together?


Usually everything has a reason to exist ... these two properties together seem strange to me, so I just want to double check.


Thank you!

Answered by QuinceyMorris in 383394022

They're functionally the same, but I recommend that you use them only as intended.


That's because they're objects, and that in turn mean that instances passed to you (by table views or collections, etc) may be subclasses that only implement one or other of these convenience properties.


For example, I recall that trying to use these for instances of NSIndexPath returned by CarPlay APIs (which use them for a slightly different purpose) crashed.


So, although the reason that they exist is so that your code reads better than without them, it's safest to use section/row for table views, section/item for collection views, and direct subscripting for everything else.

Accepted Answer

They're functionally the same, but I recommend that you use them only as intended.


That's because they're objects, and that in turn mean that instances passed to you (by table views or collections, etc) may be subclasses that only implement one or other of these convenience properties.


For example, I recall that trying to use these for instances of NSIndexPath returned by CarPlay APIs (which use them for a slightly different purpose) crashed.


So, although the reason that they exist is so that your code reads better than without them, it's safest to use section/row for table views, section/item for collection views, and direct subscripting for everything else.

Thank you QuinceyMorris!

IndexPath.row V.S. IndexPath.item?
 
 
Q