2

我需要比较来自 url 的字符串NSutf8stringencoding,以便返回 1 或 0,但即使字符串的值为 1,它也总是返回 0。

NSString *strURL = [NSString stringWithFormat:@"http://localhost/signup/login.php?username=%@&password=%@",username.text,password.text];

// to execute php code
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];

// to receive the returend value
NSString *strResult = [[NSString alloc] initWithData:dataURL 
encoding:NSUTF8StringEncoding];

// print the data in strResult
NSLog(@"%@",strResult);

if ([strResult isEqualToString:@"1"])
{
    // I need to get the control for main navigation controller 
    login2ViewController *controller = [[login2ViewController alloc] initWithNibName:@"login2ViewController" bundle:nil];
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:controller animated:YES ];

}else
{
    // invalid information
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Invalid Information" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    return;  
}

但它总是显示无效的信息,即使里面的内容strResult1。我打印了那个值NSlog。输出NSLog

2012-12-11 16:06:08.156 friend finder[277:11603] 
1

如何将该字符串与 进行比较@"1"

4

2 回答 2

1

尝试以下操作 - strResult 中可能有额外的空白字符(如回车):

if ([strResult rangeOfString:@"1"].length > 0) {
    // Do something...
}
于 2012-12-11T11:06:28.993 回答
0

字符串中可能还有其他字符。

采用

[strResult stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
于 2012-12-11T11:08:05.177 回答