繁体   English   中英

使用枚举并打开Objective-C

[英]Using Enum and Switch on Objective-C

我有3个文本字段的视图:

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

要求是,要检查这些密码的有效性,如果不满足条件,将弹出警报视图。

  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的提示)。

该代码检查两个指针​​是否相同而不是两个字符串:

if (textFromBox2 != textFromBox3) 

比较您应该使用的字符串:

[textFromBox2 isEqualToString: textFromBox3]

我认为您可以在没有枚举的情况下执行以下操作:

NSString *textFromBox1 = [stringArray objectAtIndex:0];
NSString *textFromBox2 = [stringArray objectAtIndex:1];
NSString *textFromBox3 = [stringArray objectAtIndex:2];
NSString *alertString = nil;
if ([textFromBox1 length] < 2) {
    alertString = @"現在のパスワードが間違っています。";
} else if ([textFromBox2 length] < 8) {
    alertString = @"パスワードを8文字以上で入力してください。";
} else if (![textFromBox2 isEqualToString:textFromBox3]) {
    alertString = @"新しいパスワードとパスワード(確認)が間違っています。";
}

if (alertString) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"過ち"
                                                    message:alertString
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM