1
votes

I don't understand the problem in the following line of code. I know that NSDictionary does not allow to insert nil objects so I am checking objects for nil before inserting them to NSDictionay, even after doing that crash appears.

-(NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response
{
    Request * logRequest =  [self.requestLogUtil newRequest];
            logRequest.requestParams = @{@"HTTP Method": ([request HTTPMethod] != nil ? [request HTTPMethod] : @""),
                                      @"HTTP Headers": ([request allHTTPHeaderFields] != nil ? [request allHTTPHeaderFields] : @""),
                                      @"Post Parameters": (request.HTTPBody != nil? [NSString stringWithUTF8String:[request.HTTPBody bytes]]: @""),
                                      @"Query Parameters": ([request.URL query] != nil ? [request.URL query] : @"")
                                      };
}

It crashes with following reason, but not every time.

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[2]'

1
What's the value of [NSString stringWithUTF8String:[request.HTTPBody bytes]]?Adam Wright
It is good practice to declare the variables first for a clearer code, like: NSString *postParameters = [NSString stringWithUTF8String:[request.HTTPBody bytes]]; and also check for nil like this: @"Post Parameters": postParameters ? postParameters : @""Dávid Kaszás
@KaszásDávid Thanks for your suggestion.dev gr

1 Answers

0
votes

Thanks Adam Wright!

I got it. Its because of what you have doubted about.

Due to some reason stringWithUTF8String is returning NULL.

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* +[NSString stringWithUTF8String:]: NULL cString'

I need to check for NULL as well for above method.