开发者

CFURLCreateStringByAddingPercentEscapes crashing app on line breaks

开发者 https://www.devze.com 2023-01-16 12:49 出处:网络
Code: NSString* string3 = (NSString*)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)tvQ.text, NULL, (CFStringRef)@\":/?#[]@!$&\'()*+,;=\", kCFStringEncodingUTF8);

Code:

NSString* string3 = (NSString*)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)tvQ.text, NULL, (CFStringRef)@":/?#[]@!$&'()*+,;=", kCFStringEncodingUTF8);

My code is working to pass special characters properly via URL to my database, however, if I insert data开发者_开发问答 with linebreaks, it seems to break my app and crashes. Any ideas?

**code was provided to me by user "tc." from a previous question. This is a different issue, so I started a new question.

Figured out the answer:

NSString* encoded = [[string stringByReplacingOccurrencesOfString:@"\n" withString:@" "] stringByReplacingOccurrencesOfString:@"ç" withString:@"c"];
NSString* string3 = [(NSString*)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)encoded, NULL, (CFStringRef)@":/?#[]@!$&'()*+,;=", kCFStringEncodingUTF8) autorelease];


maybe you should parse the string before calling CFURLCreateStringByAddingPercentEscapes...

A good idea is probably to use NSMutableString's method

replaceOccurrencesOfString:withString:options:range:


  1. Inside the NSString class reference, there is a section named Working with URLs.

    • stringByAddingPercentEscapesUsingEncoding:
    • stringByReplacingPercentEscapesUsingEncoding:
  2. The parameters for url works by the form:

    param1 = value1 & param2 = value2 ....

    Your sub has no value, maybe it should be removed from the param list.

0

精彩评论

暂无评论...
验证码 换一张
取 消