ios - NSUInteger oddities with for loops -


this question has answer here:

i use appcode tweak code i've written in xcode. appcode awesome code inspections, , tells things can improved.

one of frequent inspections come across points out [someobjcframeworkclass objectatindex] expecting nsuinteger in fact true...

- (objecttype)objectatindex:(nsuinteger)index 

however, keep finding myself getting screwed trying follow advice , change ints nsuintegers.

for example, here's 1 piece of code exploded when did change...

-(void)removebadge {     if ([[thebutton subviews] count]>0)     {         nsuinteger initalvalue = [[thebutton subviews] count]-1;         //get reference subview, , if it's badge, remove it's parent (the button)         (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 happening. there clue in debug data pictured below, can't make sense of it.

enter image description here

nsuinteger in unsigned, i>=0 condition in for loop evaluates yes. after i reach 0, on next iteration integer underflow, , i becomes nsuintegermax.

updated: far can tell code, there no reason in processing subviews in reverse order. so, can do

for (nsuinteger i=0; i<thebutton.subviews.count; i++) 

otherwise, can use like

if (0 == i) {     break; } 

inside loop or use do/while example.


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -