Reputation: 12592
I'm getting the common error when dealing with JSON,
-[NSNull length]: unrecognized selector sent to instance 0x102aa8b50
I completely understand this, know how to deal with it and so on (BTW here's a clever approach https://stackoverflow.com/a/16610117/294884 )
My problem is, in Xcode, how do I tell which [ length] call caused the problem??
Xcode seems to simply show it happening at "line 7 of main" - !
I'm not an expert on debugging in Xcode, what to do?
Just BTW on the specific example problem (NSNull issue), I draw your attention to the amazing library JR mentions below: github.com/jrturton/NSJSONSerialization-NSNullRemoval
Upvotes: 1
Views: 1205
Reputation: 69469
Add an "Exception Breakpoint" in the breakpoint pane of Xcode.
After you have added this all exceptions raised by code will be highlighted on the correct line, most of the time.
You are getting NSNull
object because the JSON is containing something like:
{
"someKey": null
}
Then when you get the value using [json objectForKey:@"someKey"]
it will return a NSNull
object and not nil
. Since nil
would mean that the kay was not present, which is its.
You can easily fix that with a simple Objective-C category:
NSDictionary+NotNull.h
#import <Foundation/Foundation.h>
/*! This category extends NSDictionary to work around an issue with NSNull object.
*/
@interface NSDictionary (NotNull)
/*! @abstract Returns the value associated with a given key, but only if the value is not NSNull.
@param aKey The key for which to return the corresponding value.
@return The value associated with the given key, or nil if no value is associated with the key, or the value is NSNull.
*/
- (id)objectOrNilForKey:(id)aKey;
NSDictionary+NotNull.m
#import "NSDictionary+NotNull.h"
@implementation NSDictionary (NotNull)
- (id)objectOrNilForKey:(id)aKey
{
id object = [self objectForKey:aKey];
if (object == [NSNull null]) {
return nil;
}
return object;
}
@end
Upvotes: 2