This is the code:
int index = (gridPoint.y * self.iconsPerRow) + gridPoint.x;
NSLog(@"index 1: %i", index);
NSLog(@"count: %i", [self.icons count] - 1);
if (index > [self.icons count] - 1) {
index = [self.icons count] - 1;
}
if (index < 0) {
index = 0;
}
NSLog(@"index 2: %i", index);
Output:
NSLog index 1: -4
NSLog count: 3
NSLog index 2: 3
Any ideas why this is happening? It should be resulting in 0 if it’s a minus number.
It’s because
[self.icons count];returns anNSUInteger(index is converted to an unsigned int, which will wrap around to UINT_MAX-3). Change it to the following: