Ghazalah
Ghazalah

Reputation: 223

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (NSConcreteValue)

This is my dictionary:

 { @"RoutePolyline":<__NSArrayM 0x283983750>(<c59272f7 39263940      55a4c2d8 42f65240>),
    @"RideID":6565
};

I am sending this dictionary as an argument in my API call.

and my app crashes at this line of code:

 NSData *postData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];

This is the error it throws:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (NSConcreteValue)'

I know the RoutePolyline parameter is holding a NSValue (it is supposed to be an array of coordinates) and not any object type, but I have tried converting alot, but nothing have worked so far. For example

[NSValue valueWithMKCoordinate:*routeCoordinates]

Upvotes: 0

Views: 757

Answers (2)

kubrick G
kubrick G

Reputation: 876

Loop through your NSValue array and extract CLLocationCoordinate2D's value.

for (NSValue *value in coordinates) {
    CLLocationCoordinate2D coordinate;
    [value getValue:&coordinate];
    NSDictionary *coDic = @{@"latitude" : [NSNumber numberWithDouble: coordinate.latitude],
                            @"longitude": [NSNumber numberWithDouble: coordinate.longitude]};
    [array addObject:coDic];
}

Also Check if dictionary is valid JSON before serialize

if ([NSJSONSerialization isValidJSONObject:dic]) {
    NSData *postData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];
}

Upvotes: 2

S Chauhan
S Chauhan

Reputation: 1

Get coordinates (lat longs) values first and store to an array, you can serialize then, it should not crash. Try by using NSString values in api:

NSArray *arr = @[ @{@“lat”: [NSString stringWithFormat:@"%ld",routeCoordinates.latitude],
                    @“long”:[NSString stringWithFormat:@"%ld",routeCoordinates.longitude]
                }];

Upvotes: 0

Related Questions