11

I have created a NSIndexPath object but when I print the value of its length property it will always show the value as 2.

 NSIndexPath *index = [NSIndexPath indexPathForItem:0 inSection:4];

{length = 2, path = 4 - 0}

Why length is always 2?

rishu1992
  • 1,414
  • 3
  • 14
  • 33
  • length -> length of the index path itself. 4(first)-0(second). Anyway this one already covered in answers below. – MadNik Apr 07 '16 at 05:05

2 Answers2

22

Because the index path is made of two indexes - the section and the item number.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • thank you! Is there any cases where NSIndexPath having length other than 2? – rishu1992 Apr 07 '16 at 04:58
  • It will always be 2 when used with a collection view or table view. But you can use an NSIndexPath for other things as well. So it's possible to have an index path with any number of indexes. – rmaddy Apr 07 '16 at 05:02
  • if you want to change it then it can be done in this manner myIndexPath = myIndexPath.indexPathByRemovingLastIndex() – Moin Shirazi Apr 07 '16 at 05:14
2

basically NSIndexPath is combination of Section and Row, so it will show length as 2.

Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51