Interesting bug


#1

I need to work on getting better with the debugger in xcode. :slight_smile:

Thought this was worth sharing just because the behavior was NOT what I expected.

This will work:

[code]- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection: (NSInteger) section {

int numberOfRows = [possessions count];

if ([self isEditing]) {
    numberOfRows++;
}
     
return numberOfRows;

}[/code]

This will blow up and cause a stack trace:

[code]- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection: (NSInteger) section {

int numberOfRows = [possessions count];

if ([self isEditing]) {
    return numberOfRows++;
}
     
return numberOfRows;

}[/code]

Didn’t step it threw the debugger but off the top of my head it MUST be doing a return of the value before adding one to it.

Happy coding!

Ed


#2

Hi,

That is to be expected as you’re effectively saving return the value in numberOfRows and then add one to it.

If you want to add one to numberOfRows before returning it just use the prefix version:

if ([self isEditing]) {
    return ++numberOfRows;

Gareth