使用if循环中的类实例时定义错误

时间:2013-07-22 19:13:56

标签: objective-c compiler-errors while-loop undefined

----我有两个NSString实例,其中一个是在while循环中定义的,但另一个是在那之后。 Xcode似乎认为,因为第一个实例(我们将调用string1)在while循环中,所以它不会被定义。但是,对于程序进行while循环,它将始终定义STRING1。 NSString在另一个while循环中也是如此。

----在两个while循环之外,最后,在代码中我有一个NSString的方法完成它们(isEqualtoString),但是Xcode告诉我string1和string2没有定义。该程序应该工作,但编译器阻止我。有什么我可以改变,以使string1和string2出现在Xcode的眼睛中定义。

----我正在将这个用于注册页面,我需要在while循环中使用它们,因为它们需要循环,直到用户通过控制台输入符合我要求的用户名。

编辑:在实际代码中添加。

int numb1, numb2;

char usercheck1[60];
char usercheck2[60];
//Registration
    numb2 = 1;
    while (numb2 == 1){
        numb1 = 1;
        while (numb1 == 1){
            numb1 = 0;
            NSLog(@"Username:");
            fgets(usercheck1, sizeof usercheck1, stdin);
            int c2;
            while((c2 = getchar()) != '\n' && c2 != EOF);
            if (usercheck1 [sizeof (usercheck1)-1] == '\n'){ // In case that the input string has 12 characters plus '\n'
               usercheck1 [sizeof (usercheck1)-1] = '\0';} // Plus '\0', the '\n' isn't added and the if condition is false.
            NSString* string1 = [NSString stringWithUTF8String: usercheck1];
            //Makes sure string contains no spaces and string length is correct size.
            if ([string1 length] > 12){
               NSLog (@"Username must be 12 characters or less!");
               numb1 = 1;}
            if ([string1 length] < 5){
               NSLog (@"Username must be 4 characters or more!");
               numb1 = 1;}
            if ([string1 rangeOfString:@" " ].location != NSNotFound){
               NSLog(@"Username cannot contain spaces!");
               numb1 = 1;}
      }
        numb1 = 1;
        while (numb1 == 1){
            numb1 = 0;
            NSLog(@"Confirm Username:");
            fgets(usercheck2, sizeof usercheck2, stdin);
            int c2;
            while((c2 = getchar()) != '\n' && c2 != EOF);
            if (usercheck2 [sizeof (usercheck2)-1] == '\n'){ // In case that the input string has 12 characters plus '\n'
            usercheck2 [sizeof (usercheck2)-1] = '\0';} // Plus '\0', the '\n' isn't added and the if condition is false.
            NSString* string2 = [NSString stringWithUTF8String: usercheck2];
            //Makes sure string contains no spaces and string length is correct size.
            if ([string2 length] > 12){
               NSLog (@"Username must be 12 characters or less!");
               numb1 = 1;}
            if ([string2 length] < 5){
               NSLog (@"Username must be 4 characters or more!");
               numb1 = 1;}
            if ([string2 rangeOfString:@" " ].location != NSNotFound){
               NSLog(@"Username cannot contain spaces!");
               numb1 = 1;}
        }
        if ([string2 isEqualToString: string1] == YES){
            NSLog(@"Usernames confirmed! Username:%s", string2);
            numb2 = 0;}
        else {NSLog(@"Usernames do not match. Try again");
            numb2 = 1;}
    }

}

正如您所看到的,如果它实际编译并运行它会起作用,但编译器只是不喜欢我在ifEqualToString的if语句中使用string2。它给了我错误: “使用未声明的标识符'string2'” 另外,将该语句和else语句移到两个sub-while语句之外,它为BOTH string1和string2提供了错误。

XCode版本是4.6.3,我是在10.8.4上为Mac OS X编程

1 个答案:

答案 0 :(得分:1)

您无法访问声明范围之外的变量。由于string1string2在两个while块中声明,因此您无法在while块之外使用它们。

此代码中有许多内容可以改进。尝试这样的事情:

NSString *username1;
NSString *username2;
while (1) {
    while (1) {
        NSLog(@"Username:");
        char usercheck[60];
        fgets(usercheck, sizeof usercheck1, stdin);

        int c2;
        while ((c2 = getchar()) != '\n' && c2 != EOF);
        if (usercheck [sizeof (usercheck) - 1] == '\n') { // In case that the input string has 12 characters plus '\n'
           usercheck[sizeof (usercheck)-1] = '\0';
        } // Plus '\0', the '\n' isn't added and the if condition is false.

        NSString *string1 = [NSString stringWithUTF8String:usercheck];
        // Makes sure string contains no spaces and string length is correct size.
        if ([string1 length] > 12) {
           NSLog(@"Username must be 12 characters or less!");
        } else if ([string1 length] < 5) {
           NSLog(@"Username must be 4 characters or more!");
        } else if ([string1 rangeOfString:@" "].location != NSNotFound) {
           NSLog(@"Username cannot contain spaces!");
        } else {
           username1 = string1;
           break; // username is good
        }
    }

    while (1) {
        NSLog(@"Confirm Username:");
        char usercheck[60];
        fgets(usercheck, sizeof usercheck, stdin);

        int c2;
        while ((c2 = getchar()) != '\n' && c2 != EOF);
        if (usercheck[sizeof (usercheck) - 1] == '\n') { // In case that the input string has 12 characters plus '\n'
            usercheck[sizeof (usercheck) - 1] = '\0';
        } // Plus '\0', the '\n' isn't added and the if condition is false.

        NSString *string2 = [NSString stringWithUTF8String:usercheck];
        //Makes sure string contains no spaces and string length is correct size.
        if ([string2 length] > 12) {
           NSLog (@"Username must be 12 characters or less!");
        } else if ([string2 length] < 5) {
           NSLog (@"Username must be 4 characters or more!");
        } else if ([string2 rangeOfString:@" "].location != NSNotFound) {
           NSLog(@"Username cannot contain spaces!");
        } else {
           username2 = string2;
           break;
        }
    }

    if ([username1 isEqualToString:username2]) {
        NSLog(@"Usernames confirmed! Username:%@", username1);
        break;
    } else {
        NSLog(@"Usernames do not match. Try again");
    }
}