存储在 NSDictionary 中的字符串值
我正在尝试实现此按钮操作,但 if 语句的评估结果不为 true。我遇到的情况是“学校”字典中的值并不总是存储网站。因此我想检查一下,但是我该检查什么。如果当没有值时“nil”不存储在那里,那么什么是?
-(IBAction) btnVisitWebsite_clicked :(id)sender {
if([School objectForKey:WEBSITE_KEY] == nil){
UIAlertView *NoWebsite = [[UIAlertView alloc]initWithTitle: @"No Website"
message:@"The selected school has not listed a website"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
NoWebsite.tag = 15;
[NoWebsite show];
[NoWebsite release];
NoWebsite = nil;
}
else{
NSMutableString *WebsiteVisit = [[NSMutableString alloc] initWithString: @"http://"];
[WebsiteVisit appendString:[School objectForKey:WEBSITE_KEY]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: WebsiteVisit]];
[WebsiteVisit release];
}
}
I am trying to implement this button action, but the if statement is not evaluating to be true. I have a situation where the value in that "School" dictionary will not always have a website stored. For that reason i want to check, but what do i check against. If "nil" is not stored there when there is not a value, then what is?
-(IBAction) btnVisitWebsite_clicked :(id)sender {
if([School objectForKey:WEBSITE_KEY] == nil){
UIAlertView *NoWebsite = [[UIAlertView alloc]initWithTitle: @"No Website"
message:@"The selected school has not listed a website"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
NoWebsite.tag = 15;
[NoWebsite show];
[NoWebsite release];
NoWebsite = nil;
}
else{
NSMutableString *WebsiteVisit = [[NSMutableString alloc] initWithString: @"http://"];
[WebsiteVisit appendString:[School objectForKey:WEBSITE_KEY]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: WebsiteVisit]];
[WebsiteVisit release];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果网站可能是空字符串,您应该使用以下内容:
If it is possible that the Website is an empty string you should use the following:
您是否尝试过使用 NSLog 来打印键的对象?
也许它不是零?
Have you tried to use
NSLog
to print the object for the key ?Maybe it is not nil ?
通过检查是否有条目来查看所请求的学校是否在 NSDictionary 中。
如果没有学校网站,则不应有 WEBSITE_KEY 的目录条目。
See if the requested school is in the NSDictionary by checking if it has an entry
If there is no school website, there should not be an directory entry for the WEBSITE_KEY.
NSDictionary 不能包含 nil。如果键存在,它必须包含一个对象。如果没有为特定键定义对象,则
objectForKey:
方法将返回nil
。但是,您可以将[NSNull null]
存储为字典或数组中的 null 占位符。所以你可以检查一下,这完全取决于你的字典是如何填充的。还要确保您不会将 @"" 与 nil 或无值混淆。如果它不是 nil 也不是 [NSNull null] 那么最好记录它,也许看看正在存储什么对象:
如果该类返回 NSString 或 NSCFString 那么它看起来像是包含一个空字符串。您可以检查一下。这是完整的声明:
这将很好用,因为您可能不确定字典中到底存储了什么,因为其他人将填充它。你永远都不能太小心!
An NSDictionary cannot contain nil. If the key exists it has to contain an object. The
objectForKey:
method will returnnil
if there is no object defined for a specific key. You can however store[NSNull null]
as a null placeholder in a dictionary or array. So you can check for that, it all depends how your dictionary is populated.Also ensure you're not confusing @"" with a nil or no value. If it's not nil and not [NSNull null] then it's best to log it, and perhaps look at what object is being stored:
If the class returns a NSString or NSCFString then it looks like it contains an empty string. Which you can check for. Here is the full blown statement:
This will be good to use as you may be unsure of exactly what is stored in the dictionary as others will be populating it. You can never be too careful!