开发者

string value stored in NSDictionary

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]ini开发者_开发问答tWithTitle: @"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];

}
}


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*/ }


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 ?


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.


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!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜