user3745888 user3745888 - 3 months ago 10
iOS Question

Can't I send an array in params with JSONHTTPClient?

I'm trying send some params: a NSString and an NSarray.

The

NSArray
have this format when I print this:

(54,

55)


But when I add this to params into JSONHTTPClient I get this error:


*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request parameters can be
only of NSString or NSNumber classes.


My code is:

[JSONHTTPClient postJSONFromURLWithString:uploadUrl
params:@{@"idKey":@"689769", @"idFree":myArray}
completion:^(NSDictionary *json, JSONModelError *err)

dispatch_async(dispatch_get_main_queue(), ^{

NSLog(@"complet");
});
}];


How can I send an array into params?

Answer

If the library doesn't support it, then you probably have to serialise it yourself. You can use the NSJSONSerialization Class for that: https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/#//apple_ref/occ/clm/NSJSONSerialization/dataWithJSONObject:options:error:

NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:myArray options:kNilOptions error:&error];
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

Now you have myString instead of myArray so it should be happy!