Fogmeister
Fogmeister

Reputation: 77631

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)

Upvotes: 7

Views: 6373

Answers (2)

xcodedeveloper
xcodedeveloper

Reputation: 276

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

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

Upvotes: 1

sosborn
sosborn

Reputation: 14694

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.

Upvotes: 15

Related Questions