如何比较从url到NSutf8stringencoding的字符串,返回带有正常字符串@"1"的1或0. [英] how to compare a string from url to NSutf8stringencoding return 1 or 0 with normal string @"1"
本文介绍了如何比较从url到NSutf8stringencoding的字符串,返回带有正常字符串@"1"的1或0.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用NSutf8stringencoding
比较url中的字符串以返回1或0,但是即使字符串的值为1,它也总是返回0.
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;
}
但是,即使strResult
中的内容是1
,它也始终显示无效信息.
我用NSlog
打印了该值. NSLog
的输出:
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
如何将字符串与@"1"
进行比较?
How can I compare that string with @"1"
?
推荐答案
尝试以下操作-strResult中可能会有其他空格字符(例如回车):
Try the following - there could be additional whitespace characters in the strResult (like carriage returns):
if ([strResult rangeOfString:@"1"].length > 0) {
// Do something...
}
这篇关于如何比较从url到NSutf8stringencoding的字符串,返回带有正常字符串@"1"的1或0.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文