获取“枚举值‘x’,‘y’;和'z'不在交换机中处理”事实上,这些枚举正在被处理时发出消息。发生了什么事?
我有一个这样定义的枚举:
//ModelClass.h
typedef enum { Automatic, Manual, Off } MyModes;
稍后,在 ViewController 中,我在一个方法中有一个 switch 语句,用于切换我的枚举类型的变量。我的 switch 语句如下所示:
//MyViewController.m
#import "ModelClass.h"
-(void) dryer:(MyClass *)m didChangeMode:(MyModes) newMode
{
//Code within an instance method, utilizing the above enumeration
switch (newMode) //newMode is an instance of MyMode enum.
{
case Automatic:
//Some code...
break;
case Off:
//Some code...
break;
case Manual:
//Some code...
break;
}
}
上面的 switch 语句是从我的代码中复制/粘贴的,我所做的就是用 //Some code...
注释替换功能代码。
XCode 不断在 switch (newMode)
行向我发出警告,指出“枚举值‘自动’、‘手动’和‘关闭’未在 switch 中处理”。
此外,在每个 case
语句中,我收到一条错误消息,指出“表达式不是整数常量表达式”,即使它应该是,因为它是一个枚举。
我该如何解决这个问题?奇怪的是,我在 MyModes
枚举之后的下一行定义了 ModelClass
标头(由 4 个枚举项组成)中的另一个枚举,并且在我的视图控制器中,我有另一种方法可以执行完全相同类型的 switch/case 操作。使用该枚举器,我在该方法上没有收到任何警告或错误。
可能发生什么事?
编辑
我将方法声明放在代码片段中。 newMode
变量是该方法的参数。此外,前面提到的其他正常工作的枚举器也作为参数传递到另一个方法中。
I have an enumeration defined as such:
//ModelClass.h
typedef enum { Automatic, Manual, Off } MyModes;
Later, in a ViewController, I have a switch statement within a method that switches on a variable of my enumeration type. My switch statement looks as follows:
//MyViewController.m
#import "ModelClass.h"
-(void) dryer:(MyClass *)m didChangeMode:(MyModes) newMode
{
//Code within an instance method, utilizing the above enumeration
switch (newMode) //newMode is an instance of MyMode enum.
{
case Automatic:
//Some code...
break;
case Off:
//Some code...
break;
case Manual:
//Some code...
break;
}
}
The above switch statement was copy/pasted from my code and all I did was replace the functional code with the //Some code...
comments.
XCode keeps giving me a warning at the switch (newMode)
line stating "Enumeration values 'Automatic', 'Manual', and 'Off' not handled in switch".
Furthermore, at each case
statement, I'm getting an error stating "Expression is not an integer constant expression", even though it should be since it's an enumeration.
How can I fix this? The odd thing is that I have another enumeration in the ModelClass
header (consisting of 4 enumerated items) defined on the following line after MyModes
enumeration and, in my view controller, I have another method that is doing the exact same type of switch/case operation. I am not getting a single warning or error on that method, using that enumerator.
What could be going on?
EDIT
I put the method declaration within the code snippet. The newMode
variable is a parameter to the method. Furthermore, the aforementioned, other enumerator that is working correctly is also being passed into the other method as a parameter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Keith 和 Micheal 在上面的评论中,我已经能够解决这个问题。根据基思的建议,我正在回答我自己的问题。
这个问题是我自己的错,因为我在 ViewController 中定义了一些按钮,每个按钮分别命名为
Automatic
、Manual
和Off
。编译器假定我引用的是UIButton
,而不是enum
类型,因为它们是局部范围变量。重命名枚举值并更新代码修复了此错误。就我而言,我重新定义了枚举:
With the help of both Keith and Micheal in the comments above, I have been able to solve this problem. At Keith's suggestion, I am answering my own question.
This problem was my own fault because I had defined a few buttons within my ViewController, each respectively named
Automatic
,Manual
andOff
. The compiler assumed I was referencing myUIButton
s and not theenum
type because those were local-scope variables.Renaming the enumerated values and updating the code fixed this bug. In my case, I redefined my enumeration as such: