我只能在使用xCode 4.6时重现此错误。在使用Xcode 4.5时,一切正常。
问题在于myVal具有正确的位结构,可以表示int值为-1。然而,它显示的值是4294967295,这是相同位结构如果由unsigned int表示的值。您会注意到,如果我将myVal强制转换为int,它将显示正确的值。这很奇怪,因为枚举应该首先是一个int。
以下是屏幕截图,显示了调试器中所有变量的值,在main结束时。 http://cl.ly/image/190s0a1P1b1t
typedef enum : int {
BTEnumValueNegOne = -1,
BTEnumValueZero = 0,
BTEnumValueOne = 1,
}BTEnumValue;
int main(int argc, const char * argv[])
{
@autoreleasepool {
//on this line of code myVal is 0
BTEnumValue myVal = BTEnumValueZero;
//we are adding -1 to the value of zero
myVal += BTEnumValueNegOne;
//at this moment myVal has the exact bit stucture
//of a signed int at -1, but it is displaying it
//as a unsigned int, so its value is 4294967295
//however, if i cast the enum (which should already
//be an int with a signing) to an int, it displays
//the correct value of -1
int myIntVal = (int)myVal;
}
return 0;
}
typedef NS_ENUM(BTEnumValue, NSInteger) { ... };
,看看是否会产生预期结果。 - WDUKtypedef NS_ENUM(NSInteger, BTEnumValue) { ... };
- Vitaly S.