13

When iOS 7.1 sends my viewController:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 
    cellForItemAtIndexPath:(NSIndexPath *)indexPath

The indexPath object does not recognize the properties: item, section or row. The expected values are section = 0, item = 0

The Debugger shows:

**indexPath NSIndexPath *   0xc000000000000016

 NSObject       
_indexes    NSUInteger *    NULL    
  *_indexes     
_length     
_reserved   void *  NULL

The log reports:

(lldb) po indexPath
<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}
(lldb) po indexPath.item
error: property 'item' not found on object of type 'NSIndexPath *'
error: 1 errors parsing expression
(lldb) po indexPath.row
error: property 'row' not found on object of type 'NSIndexPath *'
error: 1 errors parsing expression
(lldb) po indexPath.section
error: property 'section' not found on object of type 'NSIndexPath *'
error: 1 errors parsing expression****

Any ideas why this is happening and what to do about it?

Carl Carlson
  • 500
  • 4
  • 17
  • Could you please add your code? It's hard to see what really happens – Anton Jul 08 '14 at 02:30
  • This is the code. I mean that the results I've shown are immediately after the method is entered. Nothing else has transpired. Perhaps I don't understand the question. – Carl Carlson Jul 08 '14 at 06:12
  • You put only method name - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath without rest of code. I think there is some error. Anyway check if you put identifier to your cell. I have build a few collectionView and tableview and didn't get this issue at all. – Anton Jul 08 '14 at 09:43

3 Answers3

31

Do not use the getter/setter dot syntax, use brackets:

  1. po (int)[index row]
  2. po (int)[index section]

Note that the (int) is necessary to print the row/section as an integer rather than a hex. Other such useful formatting parameters for LLDB can be found here.

EDIT

The Swift overlay to the Foundation framework provides the IndexPath structure, which bridges to the NSIndexPath class. The IndexPath value type offers the same functionality as the NSIndexPath reference type, and the two can be used interchangeably in Swift code that interacts with Objective-C APIs. This behavior is similar to how Swift bridges standard string, numeric, and collection types to their corresponding Foundation classes.

  1. po index.row
  2. po index.section

work as expected. The comment on p vs. po still stands.

It is worth noting that you may use IndexPath in Swift instead of NSIndexPath, as described in the Apple Documentation.

Community
  • 1
  • 1
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
  • Thank you gothic dev. The problem lay elsewhere. I don't know why Log showed such strange results. Following your recommendation, the issue became clearer to me. – Carl Carlson Jul 08 '14 at 22:25
  • Check! Curious, what was it? – SwiftArchitect Jul 09 '14 at 00:00
  • I was using section and item to reference an array of arrays, but the second "array" was actually an object whose property was the array I was trying to reach. I messed up by not adding the property reference to the item object. – Carl Carlson Jul 09 '14 at 06:10
  • Doesn't matter. You are using po with items that are ints, not objects. The result is expected to be nil. – Alex Zavatone Oct 23 '14 at 20:15
11

You could try this, it works perfectly for me:

po (int)[indexPath row]
davejal
  • 6,009
  • 10
  • 39
  • 82
benzford
  • 111
  • 1
  • 4
2

Why are you using po? The resulting row and section of the NSIndexPath are NSUIntegers, or long ints, not objects.

While p means print, po means print object.

`NSIndexPath is an object. The contents of it aren't.

Use p, instead of po.

p [indexPath section]

p [indexPath row]

p indexPath.section

p indexPath.row

I make the same mistake all the time.


Note: If using a UIViewController and extending it to have a UITableViewController delegate and datasource, make sure to import UITableView.h or UIKit into your header file.

This file adds extensions to NSIndexPath so that you can use row and section. If you don't import the file that declares them, you will go mad wondering why indexpath.row and indexpath.section don't work.

If you want to access row and section of NSIndexPath outside of a UITableViewController, you will need to do those imports to get access to those extensions.

Alex Zavatone
  • 4,106
  • 36
  • 54
  • 1
    From using p instead of po -> error: no known method '-section'; cast the message send to the method's return type error: 1 errors parsing expression – richari1987 Jan 05 '15 at 22:22
  • 1
    Odd. It's possible that the NSIndexPath doesn't have the UITableView convenience methods for row and section. Which method of UITableView are you getting this in? – Alex Zavatone Jul 20 '15 at 19:40
  • I get the same results in the cellforrow delegate. – user1079052 Aug 21 '15 at 18:06
  • 1
    @AlexZavatone: my understanding is that it is not the end result that matters to `p` vs. `po`, it is the nature of how you get there. **p** prints local and global variables, whereas **po** prints the description of an ObjC object. `p` is effectively a lower level command, closer to the processor, while `po` sends a `-description` message to an object. They both should work. – SwiftArchitect Oct 19 '15 at 15:09
  • Interesting insight into how it works. That should be easy to test. Thanks. – Alex Zavatone Nov 04 '15 at 12:56
  • @AlexZavatone: your comment about convenience methods provided the answer in my case. My class was importing Foundation instead of UIKit and therefore did not recognize section and gave a build error: Property 'section' not found on object of type 'NSIndexPath *' – jk7 Oct 07 '16 at 02:16