This is my request body
[
{
"DealId":677304,
"CustomerId":328702,
"CouponQtn":1,
"PaymentType":"MPD",
"CustomerMobile":"01670234032",
"CustomerAlternateMobile":"01670234032",
"CustomerBillingAddress":"IT test order.......",
"Sizes":"not selected",
"Color":"",
"DeliveryDist":62,
"CardType":"Manual",
"OrderFrom":"iOS",
"MerchantId":14025,
"OrderSource":"app",
"AdvPaymentType":0,
"AdvPayPhoneId":0,
"deliveryCharge":45,
"OrderCouponPrice":500
}
]
I am trying to send NSMutableArray of NSMutableDictionary data using NSURLRequest in a Restful api but my app is getting exceptions like
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM bytes]: unrecognized selector sent to instance 0x600000855e40'.
My Parsing code :
-(NSDictionary*)getDataByPOST:(NSString*)url parameter:(id)parameter{
NSDictionary *dictionaryData;
NSDictionary *dic;
Reachability *reachTest = [Reachability reachabilityWithHostName:@"www.apple.com"];
NetworkStatus internetStatus = [reachTest currentReachabilityStatus];
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)){
dictionaryData = [[NSDictionary alloc] initWithObjectsAndKeys:@"error",@"status",@"No Network",@"message",nil];
dic = [NSDictionary dictionaryWithObjectsAndKeys:dictionaryData, @"response",nil];
return dic;
}
else{
NSURL *s =[self getAbsoluteURL:url];
NSMutableURLRequest *requestURL = [NSMutableURLRequest requestWithURL:s cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:900.00];
//NSLog(@"%@",requestURL);
[requestURL setHTTPMethod:@"POST"];
NSError *error=nil ;
if ([parameter isKindOfClass : [NSString class]]) {
[requestURL setHTTPBody:[parameter dataUsingEncoding:NSUTF8StringEncoding]];
}
else if([parameter isKindOfClass:[NSDictionary class]]) {
[requestURL setHTTPBody:parameter];
}
else {
[requestURL setHTTPBody:parameter];
}
NSHTTPURLResponse *response;
NSError *error1=nil;
// NSLog(@"%@\n\n%@",s,parameter);
NSData *apiData = [NSURLConnection sendSynchronousRequest:requestURL returningResponse:&response error:&error1];
if (!apiData) {
NSLog(@"Error: %@", [error localizedDescription]);
return NO;
}
if (response.statusCode == 0) {
dictionaryData = [[NSDictionary alloc] initWithObjectsAndKeys:@"error",@"status",@"Server Error",@"message",nil];
return dic;
}
else if(response.statusCode == 404) {
dictionaryData= [[NSDictionary alloc] initWithObjectsAndKeys:@"error",@"status",@"Server is Currently Down.",@"message", nil];
return dic;
}
else {
dictionaryData = [NSJSONSerialization JSONObjectWithData:apiData options:0 error:&error];
}
}
return dictionaryData;
}
And here is my api calling code
tempDic = [apiCom getNodeDataByPOST:CART_ORDER_URL parameter:orderArray];
Here orderArray is NSMutableArrray contains NSMutableDictionary objects.
Thanks
You got the error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM bytes]: unrecognized selector sent to instance 0x600000855e40'.
This means that at some point in your code you thought you could use the bytes
method (getter) on a object that has it, but in fact the object is a NSMutableArray
one, and since it doesn't implement bytes
, your code crash with that error. Now, bytes
is for instance a NSData
method. I'll get back to it.
As a developer, you need to locate the lines causing that crashes. Even if you don't understand it, if you pinpoint the issue, others might be happier to help because they can focus on the issue and not lose time looking on big portion of code hoping to find the issue. And even if it's not for others, do it for you. That's not a negative critic, that's a tip.
Culprit lines:
if ([parameter isKindOfClass : [NSString class]]) {
[requestURL setHTTPBody:[parameter dataUsingEncoding:NSUTF8StringEncoding]];
}
else if([parameter isKindOfClass:[NSDictionary class]]) {
[requestURL setHTTPBody:parameter];
}
else {
[requestURL setHTTPBody:parameter];
}
Doc of NSMutableURLRequest
:
@property(copy) NSData *HTTPBody;
So clearly, if parameter
is a NSDictionary
, or a NSArray
you'll get that same crash, with either: -[__NSDictionary bytes]: unrecognized selector sent to instance
(or similar, with a I
or a M
somewhere in the class name for Mutable/Immutable or Single
for optimized Dictionary/Array, etc.)
Now, it depends on the doc of your Web API: A common usage is to use JSON:
else if([parameter isKindOfClass:[NSDictionary class]] || [parameter isKindOfClass:[NSArray class]]) {
[request setHTTPBody:[NSJSONSerialization dataWithJSONObject:parameter options:0 error:nil]];
}
I used nil
for the error
, but it might be interesting to check its value in case of failure.