Try the following - there could be additional whitespace characters in the strResult (like carriage returns):
if ([strResult rangeOfString:@"1"].length > 0) {
// Do something...
}
質問
I need to compare a string from an url using NSutf8stringencoding
in order to return 1 or 0, but it always returns 0 even if the string has value 1.
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;
}
But it always show invalid informartion even if the content in strResult
is 1
.
I printed that value by NSlog
. The output from NSLog
:
2012-12-11 16:06:08.156 friend finder[277:11603]
1
How can I compare that string with @"1"
?
解決
Try the following - there could be additional whitespace characters in the strResult (like carriage returns):
if ([strResult rangeOfString:@"1"].length > 0) {
// Do something...
}
他のヒント
There are probably other characters in the string.
Use
[strResult stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];