2012-08-12 56 views
1

下面的代码几乎完美地工作。我在中间使用php从Xcode连接到本地主机上的mysql服务器。这到底会是一个登录系统:如果陈述总是去其他方法?

NSString *strURL = [NSString stringWithFormat:@"http://localhost:8888/Check.php?user=%@&pass=%@",txtName.text,passName.text]; 

// to execute php code 
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:strURL]]; 
NSError *e; 
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&e]; 

// to receive the returned value 
NSString *strResult = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]autorelease]; 

NSLog(@"%@",strResult); 

NSString *success = @"Success"; 

if ([strResult isEqualToString:success]) { 
NSLog(@"I realize that the two strings are the same"); 
} 

else { 
NSLog(@"The two strings are not the same"); 
} 

的strResult打印出在调试器下面,我说的是PHP文件回显到我的不同条件的项目(如用户名和密码是对还是错)

但是,由于某些原因,代码的if语句部分始终转到else方法,即使在输出中它特别指出字符串strResult包含单词“Success”。

之所以如此恼怒,因为可以看到两个字符串,(strResult和成功),是彼此相等,但由于某些原因的Xcode不能。

+0

如果语句不返回任何内容。 – hakre 2012-08-12 16:22:37

+2

查看案例敏感性 – 2012-08-12 16:23:12

+0

谢谢大家的意见。 @Codemaster Gabriel,谢谢你的建议。我希望我明白你在说什么。但是,我已经看过php文件和NSStrings的区分大小写。他们都有相同的确切“成功”作为他们的最终价值与拼写和大写相同。 – Zack 2012-08-12 16:36:12

回答

4

您的strResult可能在末尾包含空格。尝试登录这样得到字符的十六进制转储的字符串:

NSLog(@"strResult = %@", [strResult dataUsingEncoding:NSUTF8StringEncoding]); 
NSLog(@"success = %@", [success dataUsingEncoding:NSUTF8StringEncoding]); 

OR

NSLog(@"%u",strResult.length); 

如果它是一个空白的问题,你可以在这里使用的答案修剪:What's the best way to trim whitespace from a string in Cocoa Touch?

+0

非常感谢!发现这是一个空白问题,因为它返回了8个字符而不是7个字符!一切都完美无缺! – Zack 2012-08-12 16:48:03

+1

对不起,我不能+1你或我会 – Zack 2012-08-12 16:52:50