1

My app is pretty much complete and we are in the testing phase. I noticed that in our database, there are numerous json data that return null, but not a majority. Sometimes the first name data will be null and the last name will have data, or vice versa. My app crashes when it receives an instance that is NULL.

How can I catch this NULL and make an exception for it (for ALL different fields of the json data)?

user3178926
  • 339
  • 1
  • 5
  • 15
  • 1
    First of all post code that causes crash. Then describe what do you exactly want - catch exceptions? change NULLs to some default values (like empty string)? use NSNull class? – Avt Mar 24 '14 at 23:57

1 Answers1

2

There are a few possible solutions.

1) Testing for NSNull

If you just want to test for null (I'm assuming NSNull) then the following works: How can I check If I got null from json?

2) Replacing NSNulls with strings

If you're using a dictionary and you just want to replace all nulls with empty strings then this could be what you're looking for: Replace all NSNull objects in an NSDictionary

3) NSDictionary category

I personally use a category which adds a new method to NSDictionary. It just returns nil if the value is NSNull (originally from TouchJSON, dealing with NSNull)

NSDictionary+Utility.h

#import <Foundation/Foundation.h>

@interface NSDictionary (Utility)

- (id)objectForKeyNotNull:(id)key;

@end

NSDictionary+Utility.m

#import "NSDictionary+Utility.h"

@implementation NSDictionary (Utility)

- (id)objectForKeyNotNull:(id)key {
    id object = [self objectForKey:key];
    if (object == [NSNull null]){
        return nil;
    }

    return object;
}

@end

Implementation Class (.m)

#import "NSDictionary+Utility.h"

...

id success = [JSON objectForKeyNotNull:@"success"];
if (success == nil){
    /* "success" either NSNull or didn't exist in JSON. */
}
Community
  • 1
  • 1
Ian
  • 7,480
  • 2
  • 47
  • 51
  • Thanks for your help. I am fairly new to iOS so I am kind of clueless on where I should put the code you've provided. I've tried creating a new objective c class file and implementing it in the .m file but it did not work – user3178926 Mar 25 '14 at 00:25
  • No problem. I've expanded the sample of code to show how it can be used. – Ian Mar 25 '14 at 09:49
  • just to clarify, "success" is the json field from the data right? – user3178926 Mar 25 '14 at 16:49
  • @Ian how do we implement that code I mean, I created a class file called `NSDictionary` and copy pasted that code, its not working with me. why is the import like this `NSDictionary+Utility.h` –  Mar 26 '14 at 18:35
  • You don't need to create NSDictionary - it's a core Objective C class/ The code above is called a 'category'. It's a way of adding code to an existing class (it's often easier to do this than subclassing the class). The filename "NSDictionary+Utility.h" is just a convention which is used to indicate that we're adding functionality to NSDictionary. The following page has more information if you're interested: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html – Ian Mar 26 '14 at 20:01