[英]When adding 1 to NSUInteger result is multiples of 8
I have a NSUInteger
. 我有一个
NSUInteger
。 In the viedDidLoad
method I set it so 0 : 在
viedDidLoad
方法中,我将其设置为0:
@property NSUInteger *unitOne;
//In viewdidload
_unitOne = 0;
This adds one to it. 这增加了一个。 (This is in
touchesBegan
method. This code only takes place when a UIImageView
is clicked. The UIImageView
animates downwards in a loop.) (这是在
touchesBegan
方法,该代码仅发生一个时UIImageView
被点击。的UIImageView
向下动画在一个循环中)。
_unitOne++;
NSLog(@"%lu", (unsigned long) _unitOne);
When unitOne
is supposed to equal 1, the debugger always says it's 8. When it need to equal 2, it is 16, and so on. 当
unitOne
应该等于1时,调试器总是说它是8.当它需要等于2时,它是16,依此类推。 Any ideas? 有任何想法吗?
Note: If anyone needs more code, just tell me where you think it's going wrong and I'll add it. 注意:如果有人需要更多代码,请告诉我您认为哪里出错了,我会添加它。
You have a pointer to an NSUInteger
, not an NSUInteger
as such. 你有一个指向
NSUInteger
,而不是NSUInteger
。 NSUInteger
is a "primitive", "scalar" type, not an object type. NSUInteger
是一种“原始”,“标量”类型,而不是对象类型。 You do not need to use pointers to them. 您不需要使用指针。
So, you created a pointer and set it to the null pointer value. 因此,您创建了一个指针并将其设置为空指针值。
0
can always be assigned to a pointer variable and makes it the null pointer. 0
总是可以分配给指针变量并使其成为空指针。 So, you thought you were assigning an integer value to an integer variable but you weren't. 所以,你以为你是在为整数变量分配一个整数值,但事实并非如此。
Then, you incremented the pointer. 然后,您增加指针。 The semantics of C are that incrementing the pointer advances it to the "next" element of the pointed-to type.
C的语义是递增指针使其前进到指向类型的“下一个”元素。 So, the pointer was incremented not by 1 byte but by the size of
NSUInteger
. 因此,指针不是增加1个字节,而是增加
NSUInteger
的大小。 When compiling 64-bit code, NSUInteger
is 8 bytes in size. 编译64位代码时,
NSUInteger
大小为8个字节。 So, the pointer was incremented by 1 NSUInteger
which is 8 bytes. 因此,指针增加了1个
NSUInteger
,即8个字节。
Then, when you logged the instance variable, you used a type cast to convert it to unsigned long
. 然后,当您记录实例变量时,使用类型转换将其转换为
unsigned long
。 This fooled the compiler which would otherwise have warned about using %lu
with a pointer parameter. 这欺骗了编译器,否则会警告使用
%lu
和指针参数。 The value of the pointer variable was logged, but it just pointed to 8 bytes up from the null pointer (0), and so on. 记录了指针变量的值,但它只指向空指针(0)的8个字节,依此类推。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.