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 ステートメントの部分は、出力では文字列 strResult に "Success" という単語が含まれていることが明確に示されているにもかかわらず、常に else メソッドに移動します。

両方の文字列 (strResult と success) が互いに等しいことがわかりますが、何らかの理由でXcodeではそれができないため、これは非常にイライラします。

4

1 に答える 1

4

最後に空白strResultが含まれている可能性があります。文字列内の文字の 16 進ダンプを取得するには、次のようにログを記録してみてください。

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

また

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

空白の問題である場合は、次の回答を使用して削除できます: Cocoa Touch で文字列から空白を削除する最良の方法は何ですか?

于 2012-08-12T16:40:37.607 に答える