Crashing on unrecognized selector sent with SimplePost request after it works successfully.

两盒软妹~` 提交于 2019-12-12 03:35:35

问题


I've been using the SimplePost classes for several weeks and haven't had any problems. Now I'm crashing after a Request returns proper data in a Connection. I haven't (knowingly) touched the SimplePost class files. But when I run the analyzer, it now (never did before) points out the following method:

+ (NSMutableURLRequest *) urlencodedRequestWithURL:(NSURL *)url andDataDictionary:(NSDictionary *) dictionary {
   //  Create POST request
   NSMutableURLRequest *urlencodedPostRequest = [NSMutableURLRequest requestWithURL:url];
   [urlencodedPostRequest setHTTPMethod:@"POST"];
   //  Add HTTP header info
   [urlencodedPostRequest addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField: @"Content-Type"];
   //  Add POST body
   NSMutableData *POSTBody = [NSMutableData data];
   //  Add k/v to the body
   NSArray *keyArray = [dictionary allKeys];
   for( int i = 0; i < [keyArray count]; ++i ) {
       // Core Foundation function used to transform @ ==> %40 , etc
       NSString *escapedString = (__bridge NSString *) CFURLCreateStringByAddingPercentEscapes(NULL, (__bridge CFStringRef)([dictionary objectForKey:[keyArray objectAtIndex:i]]),NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8);
       [POSTBody appendData:[[NSString stringWithFormat:@"%@=%@", [keyArray objectAtIndex:i], escapedString] dataUsingEncoding:NSUTF8StringEncoding]];
       if( i < ([keyArray count] - 1) ) {
            [POSTBody appendData:[[NSString stringWithFormat:@"&"] dataUsingEncoding:NSUTF8StringEncoding]];
       }
   }
   [urlencodedPostRequest setHTTPBody:POSTBody];
   return urlencodedPostRequest;
}

And running the Analyzer shows me:


the lines continue as:

I'm having a hard time understanding what's happening. Can anyone help, please? Thanks!


回答1:


Have you tried the __bridge_transfer for escapedString?

   NSString *escapedString = (__bridge_transfer NSString *) CFURLCreateStringByAddingPercentEscapes(NULL, (__bridge CFStringRef)([dictionary objectForKey:[keyArray objectAtIndex:i]]),NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8);

cf. Correct bridging for ARC?



来源:https://stackoverflow.com/questions/13726202/crashing-on-unrecognized-selector-sent-with-simplepost-request-after-it-works-su

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!