Vishnu Rajesh
Vishnu Rajesh

Reputation: 53

why "&", "+" characters are not encoded in iOS?

I am using this following code snippet to encode the string, which i need to send to REST Api's

 NSString* content=@"Test &a=b";
 NSString* encodedString=[content stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
 NSLog(@"encodedString :%@",encodedString);

And got the following output

**Test%20&a=b**

Why "&" and "=" characters are not encoded?

But this issue is solved by using "CFURLCreateStringByAddingPercentEscapes" method

Any help please?

Upvotes: 1

Views: 95

Answers (1)

ansible
ansible

Reputation: 3579

Looks like NSASCIIStringEncoding will convert to 7-bit Ascii, which + is 43 and & is 38. Since they are valid 7-bit ASCII chars, I would not expect them to be converted with what you have. Source -

While CFURLCreateStringByAddingPercentEscapes will

replacing certain characters with the equivalent percent escape sequence based on the specified encoding

Sources:

https://developer.apple.com/library/mac/documentation/corefoundation/Reference/CFURLRef/Reference/reference.html#//apple_ref/c/func/CFURLCreateStringByAddingPercentEscapes

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/c_ref/NSASCIIStringEncoding

https://en.wikipedia.org/wiki/ASCII

Upvotes: 1

Related Questions