Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSJSONSerialization crashing app

I have a dictionary that when I log it shows...

{
    Date = "2013-04-30 17:17:18 +0000";
    Description = Kb;
    EventID = "92193e58-c04a-4233-9a6c-1332bc056b20";
    Title = Keyboard;
}

I'm trying to turn it into NSData for a JSON web service like this...

- (NSData *)JSONRepresentation
{
    NSDictionary *dictionary = [self dictionaryObject];

    NSError *jsonError;

    NSData *JSONData = [NSJSONSerialization dataWithJSONObject:dictionary
                                                       options:0
                                                         error:&jsonError];  //This is where the error occurs.

    return JSONData;
}

But every time I run it the app just crashes.

The dictionary is formed properly, the app just crashes at this line.

In AppCode I get the crash report...

EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xdefe))

In Xcode the app just stops and if I try to continue it stops with an error...

EXC_BAD_ACCESS (code=1, address=0x0)
like image 541
Fogmeister Avatar asked Apr 30 '13 17:04

Fogmeister


2 Answers

Your keys are invalid objects for converting to JSON. From the docs:

An object that may be converted to JSON must have the following properties:

The top level object is an NSArray or NSDictionary. All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull. All dictionary keys are instances of NSString. Numbers are not NaN or infinity.

like image 172
sosborn Avatar answered Nov 19 '22 16:11

sosborn


check before, if the dictionary is invalid to convert, return.:

if (![NSJSONSerialization isValidJSONObject:dictionary]) { return; }

like image 1
xcodedeveloper Avatar answered Nov 19 '22 16:11

xcodedeveloper