在Xcode 4.6中,即使枚举被定义为有符号整数,它仍然像无符号整数一样运作。

3

我只能在使用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;
}

这可能是32位/64位问题:即使您将32位的-1设置为有符号 64位整数,它也会变成正数4294967295。 - Sergey Kalinichenko
将枚举改为typedef NS_ENUM(BTEnumValue, NSInteger) { ... };,看看是否会产生预期结果。 - WDUK
2
@WDUK NS_ENUM将类型作为第一个参数,名称作为第二个参数,因此您的示例应如下所示:typedef NS_ENUM(NSInteger, BTEnumValue) { ... }; - Vitaly S.
@VitalyS。抱歉,我忘记了! - WDUK
这个问题似乎只与调试器如何显示值有关。这是你所询问的吗?还是你的程序出现了某些问题? - Ken Thomases
1个回答

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接