if 中的条件使用 && 会产生意外结果

发布于 2024-11-15 19:27:42 字数 1818 浏览 1 评论 0原文

我正在使用 JSON 解析器进行解析。我将数据存储到 searchURL 中,它是一个 NSMutableDictionary。我确实收到了 JSON 响应。

       ksearchKeyHeadline = "noresults"
       ksearchKeyNewsText = "http://172.15.12.98:8080//...... url."
       ksearchKeyIssuersText= "noresults"

我在这里设置条件:

if ([[searchURL objectForKey:KSearchKeyHeadline] isEqualToString:@"noresults"] &&
    [[searchURL objectForKey:kSearchKeyNewsText] isEqualToString:@"noresults"] &&
    [[searchURL objectForKey:kSearchKeyIssuerText] isEqualToString:@"noresults"]) {
    NSURLRequest *req;
    req=[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:kSearchBarNoResult,text.text]]];
    [[[featuredAsset viewControler] iWebview] loadRequest:req];
}
else if([searchURL objectForKey:KSearchKeyHeadline]!=@"noresults" && 
        [searchURL objectForKey:kSearchKeyNewsText]!=@"noresults" && 
        [searchURL objectForKey:kSearchKeyIssuerText]!=@"noresults"){
    NSURLRequest *req;
    req=[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithString:kSearchBarHeadlinesURL]]];
    [[[featuredAsset viewControler] iWebview] loadRequest:req];
}
else if([[searchURL objectForKey:kSearchKeyNewsText]isEqualToString:@"noresults"] && 
        [searchURL objectForKey:kSearchKeyNewsText]!=@"noresults" &&
        [[searchURL objectForKey:kSearchKeyIssuerText]isEqualToString:@"noresults"]){
    NSURLRequest *req;
    req=[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithString:kSearchBarNewsTextURL]]];
    [[[featuredAsset viewControler]iWebview]loadRequest:req];
}

问题是,它同时调用了 else if 条件。我必须调用最后一个条件。我知道 & 符号 && 将检查条件 - 当整个条件为 true 时,它​​返回 true。我做错了什么?

I am parsing with the JSON parser. I stored the data into searchURL, which is an NSMutableDictionary. I do get a JSON response.

       ksearchKeyHeadline = "noresults"
       ksearchKeyNewsText = "http://172.15.12.98:8080//...... url."
       ksearchKeyIssuersText= "noresults"

Here I am making conditions:

if ([[searchURL objectForKey:KSearchKeyHeadline] isEqualToString:@"noresults"] &&
    [[searchURL objectForKey:kSearchKeyNewsText] isEqualToString:@"noresults"] &&
    [[searchURL objectForKey:kSearchKeyIssuerText] isEqualToString:@"noresults"]) {
    NSURLRequest *req;
    req=[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:kSearchBarNoResult,text.text]]];
    [[[featuredAsset viewControler] iWebview] loadRequest:req];
}
else if([searchURL objectForKey:KSearchKeyHeadline]!=@"noresults" && 
        [searchURL objectForKey:kSearchKeyNewsText]!=@"noresults" && 
        [searchURL objectForKey:kSearchKeyIssuerText]!=@"noresults"){
    NSURLRequest *req;
    req=[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithString:kSearchBarHeadlinesURL]]];
    [[[featuredAsset viewControler] iWebview] loadRequest:req];
}
else if([[searchURL objectForKey:kSearchKeyNewsText]isEqualToString:@"noresults"] && 
        [searchURL objectForKey:kSearchKeyNewsText]!=@"noresults" &&
        [[searchURL objectForKey:kSearchKeyIssuerText]isEqualToString:@"noresults"]){
    NSURLRequest *req;
    req=[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithString:kSearchBarNewsTextURL]]];
    [[[featuredAsset viewControler]iWebview]loadRequest:req];
}

The problem is, it's calling both else if conditions. I must call the last condition. I know the ampersand && will check the condition -- when the entire condition is true, it returns true. What I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

oО清风挽发oО 2024-11-22 19:27:42

您不能像这样比较 NSString:

[searchURL objectForKey:KSearchKeyHeadline]!=@"noresults"

您需要使用 isEqualToString

[[searchURL objectForKey:KSearchKeyHeadline] isEqualToString: @"noresults"]

另外您的 ksearchKeyHeadline"noresult",而不是 "noresults “

You can't compare NSStrings like this:

[searchURL objectForKey:KSearchKeyHeadline]!=@"noresults"

you need to use isEqualToString:

[[searchURL objectForKey:KSearchKeyHeadline] isEqualToString: @"noresults"]

Also your ksearchKeyHeadline is "noresult", not "noresults".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文