开发者

How do I make sense of the error message in objective c?

开发者 https://www.devze.com 2023-02-08 04:17 出处:网络
I know I have a bug somewhere... I a very simple bug indeed, it\'s just an index out of bounds error. I used to hunt and place breakpoints in my project to see where it is. But today I decided to be s

I know I have a bug somewhere... I a very simple bug indeed, it's just an index out of bounds error. I used to hunt and place breakpoints in my project to see where it is. But today I decided to be smart and to actually learn to read the error message, because I know it's all there! But I don't know how to read it, I tried googling and none seems to give me a good explanation!

Can anyone help me read this error message?

2011-02-01 00:28:04.952 FBDiary[44083:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 26 beyond bounds [0 .. 25]'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x011dabe9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x0132f5c2 objc_exception_throw + 47
    2   CoreFoundation                      0x011d06e5 -[__NSArrayM objectAtIndex:] + 261
    3   FBDiary                             0x00007035 -[FBFriendsList tableView:heightForRowAtIndexPath:] + 101
    4   UIKit                               0x004a3334 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 2888
    5   UIKit                               0x004a1e8b -[UITableView开发者_运维百科RowData(UITableViewRowDataPrivate) _ensureSectionOffsetIsValidForSection:] + 152
    6   UIKit                               0x004a0a0c -[UITableViewRowData numberOfRows] + 145
    7   UIKit                               0x003578c2 -[UITableView noteNumberOfRowsChanged] + 132
    8   UIKit                               0x003642b8 -[UITableView reloadData] + 773
    9   UIKit                               0x04e5d1f0 -[UITableViewAccessibility(Accessibility) reloadData] + 60

EDIT: So a few minutes after posting the question I found the bug... As you said it's in the tableView:heightForRowAtIndexPath: function. However what I really wanted to know is, can I infer the line where it's causing problem from this stack trace?


there's not much to read. and as usual the stacktrace alone won't help you to fix the bug.

For this bug you need understanding of the UITableViewDatasource protocol.

the error is caused by -[FBFriendsList tableView:heightForRowAtIndexPath:], where you most likely try to access an element of the data array because you want to return the corresponding height.

So combine your knowledge about UITableViewDatasource with your exception.
Your knowledge about the datasource should tell you that a tableview asks for heightForRowAtIndexPath for all cells currently visible.
It should also tell you that you've told the tableview how many rows it should access.

If you combine this knowledge and your exception you should come to the conclusion that the value you return in - (NSInteger)tableView:(UITableView *)tableView_ numberOfRowsInSection:(NSInteger)section is wrong, and most likely it's 26 where it should be 25.

This is the most common bug that happens with UITableView.


The interesting part is [FBFriendsList tableView:heightForRowAtIndexPath:] which is the (one and only) part of your code wich is part of this stack trace, so you have to look there to find the error.

The lines above are the access to the array and the resulting progress of throwing the exception. The lines below are UIKit's base classes that "host" your application code and are responsible for calling the right parts at the right time.


It looks like the callback for the FBFriendsList height for row is passing an NSIndexPath that is out of bounds of your array. Where ever you have your FBFriendsList you need to put a breakpoint and check the number of items in your array and also check the NSIndexPath that is getting passed.

0

精彩评论

暂无评论...
验证码 换一张
取 消