如果其他条件不能正常工作总是运行其他

时间:2013-05-07 11:13:57

标签: iphone ios if-statement

我正在使用if else条件来检查变量,但是当值为true时它总是运行,那么它也会显示其他

NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"%@",data);

if ([data isEqualToString:@"Yes"]) {

    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Welcome" message:@"Successful Login" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];

}  
else {
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Incorrect user name or password" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];

}

2 个答案:

答案 0 :(得分:1)

试试这段代码,

 NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
    data = [data stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSLog(@"%@",data);

    if ([data caseInsensitiveCompare:@"Yes"] == NSOrderedSame) {

        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Welcome" message:@"Successful Login" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];

    }
    else {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Incorrect user name or password" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];

    }

答案 1 :(得分:0)

将此用作:

 if ([[data stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isEqualToString:@"Yes"]) 
{

}

编辑:我的代码工作正常,因为我在最后检查了它。

希望它对你有所帮助。