Here’s my code:
- (IBAction)NextTouched:(id)sender {
NSLog(@"Index = %i", index);
if([project getCount]>(index++)) {
[self setUI:index];
}
}
Index is an integer, as declared in my .h file:
@property (nonatomic) int *index;
But every time I click the button, the log says the integer is going up by 4. Can you tell my why?
The reason it’s going up by 4 is because
indexis a pointer. When you increment a pointer its value increases by the size of the data type it points to, in this case anint, which is 4 bytes.Given
indexappears to be an index into anNSArray(or some other collection class), I think you want to make itintand notint *to solve your issue. Better still make it unsigned, likeNSUInteger, which is the type returned from thecountmethod.Also I think you’ll want to use prefix-increment rather than postfix-increment so that the
iftest uses the newly incremented value, not the previous value.