开发者

Unexpected results from conditions in if, using &&

开发者 https://www.devze.com 2023-03-12 18:09 出处:网络
I am parsing with the JSON parser. I stored the data into searchURL, which is an NSMutableDictionary. I do get a JSON response.

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" 开发者_StackOverflow&& 
        [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?


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".

0

精彩评论

暂无评论...
验证码 换一张
取 消