2

I'm working on an App that returns some JSON data that the user is searching for in a tableView. Sometimes there is an empty key that is returned and it seems to be crashing my app if a selection is choosin. I've looked up other solutions, but its still crashing regardless of how I set up my "if" check.

This first one is weird, it works but in the text view where the data is displayed "{}" is displayed instead of "Not Available..." Why?

if ([gameDetails objectForKey:@"description"] !=nil) {
    gameDescription.text = [NSString stringWithFormat:@"%@", [gameDetails objectForKey:@"description"]];
}
else
{
    gameDescription.text = [NSString stringWithFormat:@"Not Available..."];
}

The next one crashes regardless of how I set it up.

if ([gameDetails objectForKey:@"box_front"] != nil) {
    NSURL *coverImage = [NSURL URLWithString:[gameDetails objectForKey:@"box_front"]];
    NSData *imageLink = [NSData dataWithContentsOfURL:coverImage];
    coverArt.image = [UIImage imageWithData:imageLink];
}
else
{
    NSLog(@"No Photo");
}

Can someone tell me why these are causing my app to keep crashing? Let me know if I need more information.

8
  • In the 1st case you get an empty dictionary for the description key. In the 2nd case you don't mention the error but most likely you are getting an NSNull object. Commented Mar 26, 2014 at 4:57
  • can you print the crashing log here ? Commented Mar 26, 2014 at 4:58
  • for 2nd point 1st you have to do enscapeencoding url and then get data from URL. and you must check if you get data then only set image in imageview like if(imageLink)coverArt.image = [UIImage imageWithData:imageLink]; Commented Mar 26, 2014 at 4:59
  • Rmaddy: Since I'm getting an empty dictionary for the description key, why is it putting {} instead of the text I want? Commented Mar 26, 2014 at 5:05
  • Because the output of NSDictionary description on an empty dictionary will be {}. BTW - when you wish to reply to a specific person, put the @ symbol before their username. Commented Mar 26, 2014 at 5:12

2 Answers 2

4
NSURL *coverImage = [NSURL URLWithString:[gameDetails objectForKey:@"box_front"]];

// you have to check that coverImage is not nil then only you have to do following operation. else write some info regarding nil.

NSData *imageLink = [NSData dataWithContentsOfURL:coverImage];
coverArt.image = [UIImage imageWithData:imageLink];

// For checking nil or null you can use following code.

if([[itemDataDict valueForKey:@"ItemNo"] isKindOfClass:[NSString class]])
    {
        if([[itemDataDict valueForKey:@"ItemNo"] isEqualToString:@""] || [[itemDataDict valueForKey:@"ItemNo"] isEqualToString:@"<null>"])
        {
            txtItemNo.text = @"";
        }
        else
        {
            txtItemNo.text = [NSString stringWithFormat:@"%@",[itemDataDict valueForKey:@"ItemNo"]];
        }
    }
else
    {
        txtItemNo.text = @"";
    }

Let me know if you required more information.

Sign up to request clarification or add additional context in comments.

2 Comments

Can this same solution be applied to my first problem? Since it's returning an empty dictionary not nil.
Yes you can apply this solution to check empty dictionary, need to change the condition as per your requirement.
1

It crashes second time due to the line below

NSURL *coverImage = [NSURL URLWithString:[gameDetails objectForKey:@"box_front"]];

and

if ([gameDetails objectForKey:@"box_front"] != nil)

Your if condition is not false always, sometime you get some other value like (null) or, NSNull, which is different from nil and the NSURL statement will crash if you supply anything other than NSString.

To avoid this condition you can have a check on [gameDetails objectForKey:@"box_front"] that what you are getting..

I suggest you can use some macro like below

#define IsEmpty(value) (value == (id)[NSNull null] || value == nil || ([value isKindOfClass:[NSString class]] && ([value isEqualToString:@""] ||  [value isEqualToString:@"<null>"]))) ? YES : NO

#define IfNULL(original, replacement) IsNULL(original) ? replacement : original

#define IsNULL(original) original == (id)[NSNull null]

#define SafeString(value) IfNULL(value, @"")

and then use the check below

if (SafeString([gameDetails objectForKey:@"box_front"]).length != 0)

I hope it works.

Cheers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.