Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For Loop Objective C

Why do I get this as a result to this code?

CODE

ids = 0;
for (NSString *s in golferThreeIconCounter) {
    ids++;
    NSLog(@"%i", ids);
}

RESULT

2012-05-24 16:30:35.194 Dot Golf Scoring[673:f803] 4
2012-05-24 16:30:35.196 Dot Golf Scoring[673:f803] 8
2012-05-24 16:30:35.196 Dot Golf Scoring[673:f803] 12
2012-05-24 16:30:35.197 Dot Golf Scoring[673:f803] 16
2012-05-24 16:30:35.197 Dot Golf Scoring[673:f803] 20
2012-05-24 16:30:35.198 Dot Golf Scoring[673:f803] 24
2012-05-24 16:30:35.199 Dot Golf Scoring[673:f803] 28
2012-05-24 16:30:35.199 Dot Golf Scoring[673:f803] 32
2012-05-24 16:30:35.200 Dot Golf Scoring[673:f803] 36
2012-05-24 16:30:35.200 Dot Golf Scoring[673:f803] 40
2012-05-24 16:30:35.201 Dot Golf Scoring[673:f803] 44
2012-05-24 16:30:35.201 Dot Golf Scoring[673:f803] 48
2012-05-24 16:30:35.202 Dot Golf Scoring[673:f803] 52
2012-05-24 16:30:35.202 Dot Golf Scoring[673:f803] 56
2012-05-24 16:30:35.203 Dot Golf Scoring[673:f803] 60

It makes absolutely no sense to me why ids goes up 4 times instead of just once...

like image 474
The Man Avatar asked Dec 06 '22 14:12

The Man


1 Answers

When you declare an int, you do not add *: it's not an <id> type. What you have is a pointer to an int; on a 32-bit platform it increments by 4.

int ids = 0;
like image 93
Sergey Kalinichenko Avatar answered Dec 28 '22 09:12

Sergey Kalinichenko