-1

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

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Logicsaurus Rex
  • 3,172
  • 2
  • 18
  • 27
  • 1
    For an *unsigned* integer, `i>=0` is *always* true. The loop never terminates and the integer "wraps around". – Martin R Sep 07 '16 at 18:58
  • 1
    "AppCode does awesome code inspections" I guess they aren't so awesome after all. – matt Sep 07 '16 at 19:00

1 Answers1

2

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.

Borys Verebskyi
  • 4,160
  • 6
  • 28
  • 42