Objective c 使用if循环中类的实例时发生定义错误

Objective c 使用if循环中类的实例时发生定义错误,objective-c,compiler-errors,while-loop,undefined,Objective C,Compiler Errors,While Loop,Undefined,----我有两个NSString实例,其中一个在while循环中定义,但另一个在while循环之后。Xcode似乎认为,由于第一个实例(我们将称之为string1)处于while循环中,因此不会定义它。然而,为了让程序从while循环中继续,它将始终定义STRING1。NSString位于另一个while循环中,这是相同的事情。 ----在这两个while循环之外,最后,在代码中,我对它们都使用了NSString方法(isEqualtoString),但是Xcode告诉我没有定义string1和

----我有两个NSString实例,其中一个在while循环中定义,但另一个在while循环之后。Xcode似乎认为,由于第一个实例(我们将称之为string1)处于while循环中,因此不会定义它。然而,为了让程序从while循环中继续,它将始终定义STRING1。NSString位于另一个while循环中,这是相同的事情。

----在这两个while循环之外,最后,在代码中,我对它们都使用了NSString方法(isEqualtoString),但是Xcode告诉我没有定义string1和stringtwo。程序应该可以运行,但编译器阻止了我。是否有什么我可以更改的,以使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;}
    }

}
int numb1,numb2;
char usercheck1[60];
char usercheck2[60];
//登记
numb2=1;
while(numb2==1){
numb1=1;
while(numb1==1){
numb1=0;
NSLog(@“用户名:”);
fgets(usercheck1,usercheck1的大小,stdin);
int c2;
而((c2=getchar())!='\n'&&c2!=EOF);
如果(usercheck1[sizeof(usercheck1)-1]='\n'){//输入字符串有12个字符加'\n'
usercheck1[sizeof(usercheck1)-1]='\0';}//Plus'\0',不添加'\n',if条件为false。
NSString*string1=[NSString stringWithUTF8String:usercheck1];
//确保字符串不包含空格,且字符串长度大小正确。
如果([string1长度]>12){
NSLog(@“用户名必须不超过12个字符!”);
numb1=1;}
如果([string1长度]<5){
NSLog(@“用户名必须为4个或更多字符!”);
numb1=1;}
if([string1 rangeOfString:@”“).location!=NSNotFound){
NSLog(@“用户名不能包含空格!”);
numb1=1;}
}
numb1=1;
while(numb1==1){
numb1=0;
NSLog(@“确认用户名:”);
fgets(usercheck2,usercheck2的大小,stdin);
int c2;
而((c2=getchar())!='\n'&&c2!=EOF);
如果(usercheck2[sizeof(usercheck2)-1]='\n'){//输入字符串有12个字符加'\n'
usercheck2[sizeof(usercheck2)-1]='\0';}//加上'\0',不添加'\n',且if条件为false。
NSString*string2=[NSString stringWithUTF8String:usercheck2];
//确保字符串不包含空格,且字符串长度大小正确。
如果([string2长度]>12){
NSLog(@“用户名必须不超过12个字符!”);
numb1=1;}
如果([string2长度]<5){
NSLog(@“用户名必须为4个或更多字符!”);
numb1=1;}
if([string2 rangeOfString:@”“).location!=NSNotFound){
NSLog(@“用户名不能包含空格!”);
numb1=1;}
}
如果([string2 isEqualToString:string1]==是){
NSLog(@“用户名已确认!用户名:%s”,字符串2);
numb2=0;}
else{NSLog(@“用户名不匹配。请重试”);
numb2=1;}
}
}
正如您所看到的,如果它真的编译并运行,它会工作,但是编译器不喜欢我在isEqualToString的if语句中使用string2。它给了我一个错误: “使用未声明的标识符'string2'” 另外,将该语句和else语句移到两个sub-while语句之外,它会给出string1和string2的错误


XCode版本是4.6.3,我正在为10.8.4上的MacOSX编程,您不能访问声明范围之外的变量。由于
string1
string2
在两个
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");
    }
}
NSString*username1;
NSString*username2;
而(1){
而(1){
NSLog(@“用户名:”);
char usercheck[60];
fgets(usercheck、usercheck1的大小、标准输入);
int c2;
而((c2=getchar())!='\n'&&c2!=EOF);
如果(usercheck[sizeof(usercheck)-1]='\n'){//输入字符串有12个字符加'\n'
usercheck[sizeof(usercheck)-1]='\0';
}//加上'\0',则不添加'\n',且if条件为false。
NSString*string1=[NSString stringWithUTF8String:usercheck];
//确保字符串不包含空格,且字符串长度大小正确。
如果([string1长度]>12){
NSLog(@“用户名必须不超过12个字符!”);
}否则如果([string1长度]<5){
NSLog(@“用户名必须为4个或更多字符!”);
}else if([string1 rangeOfString:@”“).location!=NSNotFound){
NSLog(@“用户名不能包含空格!”);
}否则{
username1=string1;
break;//用户名是好的
}
}
而(1){
NSLog(@“确认用户名:”);
char usercheck[60];
fgets(用户检查、用户检查大小、标准输入);
int c2;
而((c2=getchar())!='\n'&&c2!=EOF);
如果(usercheck[sizeof(usercheck)-1]='\n'){//输入字符串有12个字符加'\n'
usercheck[sizeof(usercheck)-1]='\0';
}//加上'\0',则不添加'\n',且if条件为false。
NSString*string2=[NSString stringWithUTF8String:usercheck];
//确保字符串不包含sp