存储在 NSDictionary 中的字符串值

发布于 2024-08-18 00:58:35 字数 986 浏览 7 评论 0原文

我正在尝试实现此按钮操作,但 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 技术交流群。

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

发布评论

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

评论(4

温柔戏命师 2024-08-25 00:58:35

如果网站可能是空字符串,您应该使用以下内容:

if(![School objectForKey:WEBSITE_KEY] 
   || ([[School objectForKey:WEBSITE_KEY] isKindOfClass:[NSString class]] && [(NSString*)[School objectForKey:WEBSITE_KEY] length]==0)){
    // no website
} else { /* has website*/ }

If it is possible that the Website is an empty string you should use the following:

if(![School objectForKey:WEBSITE_KEY] 
   || ([[School objectForKey:WEBSITE_KEY] isKindOfClass:[NSString class]] && [(NSString*)[School objectForKey:WEBSITE_KEY] length]==0)){
    // no website
} else { /* has website*/ }
千笙结 2024-08-25 00:58:35

您是否尝试过使用 NSLog 来打印键的对象?

NSLog(@"School[WEBSITE_KEY]=<%@>", [School objectForKey:WEBSITE_KEY]);

也许它不是零?

Have you tried to use NSLog to print the object for the key ?

NSLog(@"School[WEBSITE_KEY]=<%@>", [School objectForKey:WEBSITE_KEY]);

Maybe it is not nil ?

梦开始←不甜 2024-08-25 00:58:35

通过检查是否有条目来查看所请求的学校是否在 NSDictionary 中。

BOOL containsKey = [[School allKeys] containsObject:WEBSITE_KEY];

如果没有学校网站,则不应有 WEBSITE_KEY 的目录条目。

See if the requested school is in the NSDictionary by checking if it has an entry

BOOL containsKey = [[School allKeys] containsObject:WEBSITE_KEY];

If there is no school website, there should not be an directory entry for the WEBSITE_KEY.

反话 2024-08-25 00:58:35

NSDictionary 不能包含 nil。如果键存在,它必须包含一个对象。如果没有为特定键定义对象,则 objectForKey: 方法将返回 nil。但是,您可以将 [NSNull null] 存储为字典或数组中的 null 占位符。所以你可以检查一下,这完全取决于你的字典是如何填充的。

[School objectForKey:WEBSITE_KEY] == nil || [School objectForKey:WEBSITE_KEY] == [NSNull null]

还要确保您不会将 @"" 与 nil 或无值混淆。如果它不是 nil 也不是 [NSNull null] 那么最好记录它,也许看看正在存储什么对象:

NSLog(@"Description: %@", [School objectForKey:WEBSITE_KEY]);
NSLog(@"Class: %@", [[School objectForKey:WEBSITE_KEY] class]);

如果该类返回 NSString 或 NSCFString 那么它看起来像是包含一个空字符串。您可以检查一下。这是完整的声明:

id schoolWebsite = School objectForKey:WEBSITE_KEY];
if (schoolWebsite && schoolWebsite != [NSNull null] && [schoolWebsite isKindOfClass:[NSString class]] && [schoolWebsite length] > 0)) {
    // There is definitely a non-empty string for that key!
} else {
    // Not valid
}

这将很好用,因为您可能不确定字典中到底存储了什么,因为其他人将填充它。你永远都不能太小心!

An NSDictionary cannot contain nil. If the key exists it has to contain an object. The objectForKey: method will return nil 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.

[School objectForKey:WEBSITE_KEY] == nil || [School objectForKey:WEBSITE_KEY] == [NSNull null]

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:

NSLog(@"Description: %@", [School objectForKey:WEBSITE_KEY]);
NSLog(@"Class: %@", [[School objectForKey:WEBSITE_KEY] class]);

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:

id schoolWebsite = School objectForKey:WEBSITE_KEY];
if (schoolWebsite && schoolWebsite != [NSNull null] && [schoolWebsite isKindOfClass:[NSString class]] && [schoolWebsite length] > 0)) {
    // There is definitely a non-empty string for that key!
} else {
    // Not valid
}

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!

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