objective-c – 为什么isEqualToString对NSString不起作用?
发布时间:2020-12-15 01:42:49 所属栏目:百科 来源:网络整理
导读:我正在尝试在XCode中为iPhone应用程序的登录过程编写代码.问题在于下面的NSString serverOutput.当我使用printf(serverOutput.UTF8String)打印它时;它向控制台打
我正在尝试在XCode中为iPhone应用程序的登录过程编写代码.问题在于下面的NSString serverOutput.当我使用printf(serverOutput.UTF8String)打印它时;它向控制台打印“是”.但是,当我将serverOutput与“Yes”进行比较时,它不起作用.任何帮助,将不胜感激.这是我的代码:
- (IBAction) loginButton: (id) sender { // TODO: spawn a login thread indicator.hidden = FALSE; [indicator startAnimating]; NSString *post =[NSString stringWithFormat:@"username=%@&password=%@",userName.text,password.text]; NSString *hostStr = @"http://10.243.1.184/connectDB.php?"; hostStr = [hostStr stringByAppendingString:post]; NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]]; NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding]; printf(serverOutput.UTF8String); if([serverOutput isEqualToString:@"Yes"]){ UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Congrats" message:@"You are authorized" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil,nil]; [alertsuccess show]; [alertsuccess release]; } else { UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Username or Password Incorrect" delegate:self cancelButtonTitle:@"OK"otherButtonTitles:nil,nil]; [alertsuccess show]; [alertsuccess release]; //[self.navigationController pushViewController:DashboardViewController animated:YES]; loginbutton.enabled = TRUE; } loginbutton.enabled = FALSE; } 解决方法
基于帮助处于类似情况的其他人,我会说问题是来自服务器的响应不仅仅是字符串“是”.很可能在文本之前和/或之后有一些空格.也许是一两个流浪换行.
尝试这个: NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding]; NSLog(@"Result = '%@'",serverOutput); // look for space between quotes serverOutput = [serverOutput stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |