How can I catch NULL exceptions in iOS from json -
my app pretty complete , in testing phase. noticed in our database, there numerous json data return null, not majority. first name data null , last name have data, or vice versa. app crashes when receives instance null.
how can catch null , make exception (for different fields of json data)?
there few possible solutions.
1) testing nsnull
if want test null (i'm assuming nsnull) following works: how can check if got null json?
2) replacing nsnulls strings
if you're using dictionary , want replace nulls empty strings you're looking for: replace nsnull objects in nsdictionary
3) nsdictionary category
i use category adds new method nsdictionary. returns nil if value nsnull (originally touchjson, dealing 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. */ }
Comments
Post a Comment