I am trying to increment a variable in Objective-C. This would seem to be an easy:
dataPos++
but when I do this, the value increases very strangely:
dataPos: 1,
dataPos: 2,
dataPos: 3,
dataPos: 4,
dataPos: 12,
dataPos: 185273100`
This is my code:
int dataPos = 0;
uint8_t temparray[1];
for (int x=0; x < mapX; x++) {
for (int y=0; y < mapY; y++) {
for (int z=0; z < mapZ; z++) {
[mapdata getBytes:&temparray range:NSMakeRange(dataPos, dataPos+1)];
dataPos++;
NSLog(@"dataPos: %d", dataPos);
map[x][y][z] = temparray[0];
}
}
}
mapData is an NSMutableData, with about 4194308 bytes in it, and map[][][] is a uint8_t array.
I have tried cleaning the build folder, building on both the simulator (Value never increases, stays at 1), and the device.
Any help is much appreciated.
NSMakeRangetakes a position and length, not a start and end position. You’re likely reading too much data intotemparrayand overwritingdataPosin the process. See the Foundation Functions Reference for more info, and rewrite your code to read: