我正在使用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];
}
答案 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"])
{
}
编辑:我的代码工作正常,因为我在最后检查了它。
希望它对你有所帮助。