ios - NSUInteger oddities with for loops -
this question has answer here:
- nsuinteger in reversed loop confusion? 2 answers
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.
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
Post a Comment