问题描述
如何将网站结果与预测结果进行比较。
How do i compare a website result with a predicted result.
@"document.getElementsByTagName('body')[0].outerHTML"
预计包含:
<body>OK</body>
但我总是得到一个错误,说它们不匹配。我使用下面的代码来比较它们:
But i always get an error saying that they don't match. I used this code below to compare them:
if (webresult == cmp){
然后它会显示一条说明成功的警报。或者在其他地方它会说错误。它总是走向别的。下面是代码块,请帮忙。
then it shows an alert saying success. Or in else it'll say error. It always goes to else. Heres the code block, Please help.
- (IBAction)displayresult:(id)sender {
webresult = [webview2 stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].outerHTML"];
NSString *cmp = [[NSString alloc] initWithFormat:@"<body>OK</body>"];
if (webresult == cmp) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Logged in" message:@"Logged in, Proceeding to the game" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:webresult delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
}
推荐答案
我假设 webresult
是 NSString
。如果是这种情况,那么你想使用:
I assume that webresult
is an NSString
. If that is the case, then you want to use:
if ([webresult isEqualToString:cmp]) {
而不是:
if (webresult == cmp) {
,因为上述方法检查字符串是否相等按字符,而bottom方法检查两个字符串是否是相同的指针。希望有所帮助!
as the above method checks if the strings are equal character by character, whereas the bottom method checks if the two strings are the same pointer. Hope that helps!
这篇关于xcode iOS比较字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!