ios - 在 Objective-C 上使用 Enum 和 Switch

标签 ios objective-c if-statement enums switch-statement

我有一个包含 3 个文本字段的 View :

  1. 当前密码
  2. 新密码
  3. 确认新密码

要求是,检查这些密码的有效性,如果不满足条件,会有一个alert view popup。

  1. 如果用户输入错误的当前密码
  2. 新密码必须超过8个字母
  3. 新密码必须匹配

我用了if-else但是好像不太合适,改用switch-case .但它仍然不起作用。

我的代码:

NSString *textFromBox1 = [stringArray objectAtIndex:0];
NSString *textFromBox2 = [stringArray objectAtIndex:1];
NSString *textFromBox3 = [stringArray objectAtIndex:2];

enum ErrorCase{
    oldPasswordCase,
    newPasswordLength,
    newPasswordCheck,
};

enum ErrorCase error;

if ([textFromBox1 length] < 2) {
    error = oldPasswordCase;
} else if ([textFromBox2 length] < 8) {
    error = newPasswordLength;
} else if (textFromBox2 != textFromBox3) {
    error = newPasswordCheck;
}

switch (error) {
    case oldPasswordCase:
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"過ち"
                                                        message:@"現在のパスワードが間違っています。"
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }
        break;
    case newPasswordLength:
    {
        UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"過ち"
                                                         message:@"パスワードを8文字以上で入力してください。"
                                                        delegate:self
                                               cancelButtonTitle:@"OK"
                                               otherButtonTitles:nil];
        [alert2 show];

    }
        break;
    case newPasswordCheck:
    {
        UIAlertView *alert3 = [[UIAlertView alloc] initWithTitle:@"過ち"
                                                         message:@"新しいパスワードとパスワード(確認)が間違っています。"
                                                        delegate:self
                                               cancelButtonTitle:@"OK"
                                               otherButtonTitles:nil];
        [alert3 show];
    }
        break;

    default:
        break;
}

最佳答案

在 Objective-C 中,switch 语句中的参数必须是整数。

使用 NS_ENUM 宏将枚举声明为整数

typedef NS_ENUM (NSInteger, ErrorCase) {
    oldPasswordCase,
    newPasswordLength,
    newPasswordCheck,
};

然后声明错误变量

ErrorCase error;

正如 Mateusz 的回答中提到的,您必须使用 isEqualToString 来比较两个字符串。

编辑:您的 if - else 链并不详尽,这可能会导致意外行为。要么添加默认的 none 情况并将其分配给 error 要么确保 if - else 链考虑所有情况(感谢 Idali提示)。

关于ios - 在 Objective-C 上使用 Enum 和 Switch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38866715/

相关文章:

java - 如何检查变量是否为空?

ios - 使用 dispatch_async() 避免 View 阻塞的解决方法是什么

ios - 无法将 _NSCFNumber 转换为 int

ios - 如何更改表格 View 单元格图像?

iphone - UILabel截断 "..."(空格+...)而不是 "..."

c++ - c++语言/getdate()中如何定义一个表示时间值的值?

java - java中的数字重复到下一行

android - 链接器剥离未使用的类

ios - UIPopoverPresentationController : multiple permittedArrowDirections order priority

javascript - 未捕获的 SyntaxError : Unexpected token else, 完全被难住了