iosobjective-cnsuinteger

NSUInteger oddities with for loops


I use AppCode to tweak the code I've written in XCode. AppCode does awesome code inspections, and tells you where things can be improved.

One of the frequent inspections I come across points out that [SomeObjCFrameworkClass objectAtIndex] is expecting an NSUInteger which is in fact true...

- (ObjectType)objectAtIndex:(NSUInteger)index

however, I keep finding myself getting screwed by trying to follow this advice and change my ints to NSUIntegers.

For example, here's one piece of code that exploded when I did this change...

-(void)removeBadge
{
    if ([[theButton subviews] count]>0)
    {
        NSUInteger initalValue = [[theButton subviews] count]-1;
        //Get reference to the subview, and if it's a badge, remove it from it's parent (the button)
        for (NSUInteger i=initalValue; i>=0; i--) {

            if ([[[theButton subviews] objectAtIndex:i] isMemberOfClass:[MKNumberBadgeView class]])
            {
                [[[theButton subviews] objectAtIndex:i] removeFromSuperview];
                [theButton setTitleColor:[UIColor lightTextColor] forState:UIControlStateNormal];
            }
        }
    }
}

Any idea why this is happening. There is a clue in the debug data pictured below, but I can't make sense of it.

enter image description here


Solution

  • NSUInteger in unsigned, so i>=0 condition in your for loop always evaluates to YES. After i reach 0, on next iteration you will get integer underflow, and i becomes NSUIntegerMax.

    Updated: As far as I can tell from your code, there is no reason in processing subviews in reverse order. So, you can simply do

    for (NSUInteger i=0; i<theButton.subviews.count; i++)
    

    Otherwise, you can use something like

    if (0 == i) {
        break;
    }
    

    inside your loop or use do/while for example.