[英]Enum type used in switch case error
I wrote following code in my app: 我在我的应用程序中写了以下代码:
typedef enum{
PasswordModeEnter = 1,
PasswordModeSetNewPassword = 3,
passwordModeVerify = 5,
PasswordModeChange = 7,
PasswordModeDisabled = 9
}PasswordModes;
And I instantiated an object named PasswordMode. 我实例化了一个名为PasswordMode的对象。
- (id)initWithPasswordMode:(PasswordModes *)passwordMode nibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.PasswordMode = passwordMode;
}
return self;
}
I got an strange error when I wrote following code: 我编写以下代码时遇到了一个奇怪的错误:
switch (self.PasswordMode) {
case PasswordModeChange:
//do something
break;
case PasswordModeDisabled:
//do something
break;
case PasswordModeEnter:
//do something
break;
case PasswordModeSetNewPassword:
//do something
break;
case passwordModeVerify:
//do something
break;
default:
break;
}
I got following error: 我收到以下错误:
statement requires expression of integer type.
语句需要表达整数类型。
what is the problem and how can I solve it? 问题是什么,我该如何解决?
The problem is with this 问题在于此
ProblemModes *
The * should be removed. *应该删除。
PasswordModes is just an enum, your init method incorrectly indicates that it is a pointer. PasswordModes只是一个枚举,你的init方法错误地指出它是一个指针。 The init method signature should be as follows:
init方法签名应如下所示:
- (id)initWithPasswordMode:(PasswordModes)passwordMode nibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
Your property should be as follows: 您的财产应如下:
@property (nonatomic,assign) PasswordMode passwordMode;
you should specify the type of the enum, also you might want to use the new enum syntax. 你应该指定枚举的类型,也可以使用新的枚举语法。
typedef NS_ENUM(NSInteger, PasswordMode) {
PasswordModeEnter = 1,
PasswordModeSetNewPassword = 3,
passwordModeVerify = 5,
PasswordModeChange = 7,
PasswordModeDisabled = 9
};
You can use it as follows: 您可以按如下方式使用它:
PasswordMode mode = PasswordModeEnter;
switch (mode) {
case PasswordModeEnter:
break;
...
case PasswordModeDisabled:
break;
default:
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.