2012-08-12 42 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