if 中的条件使用 && 会产生意外结果
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能像这样比较 NSString:
您需要使用
isEqualToString
:另外您的
ksearchKeyHeadline
是"noresult"
,而不是"noresults “
。You can't compare NSStrings like this:
you need to use
isEqualToString
:Also your
ksearchKeyHeadline
is"noresult"
, not"noresults"
.