StringByAddingPercentEscapesUsingEncoding does not work with NSStrings with '0'

I am having a problem with the stringByAddingPercentEscapesUsingEncoding: method. Here's what happens:

When I try to use a method to convert NSString:

 "..City=Cl&PostalCode=Rh6 0Nt" 

I understand it.

 "City=Cl&PostalCode=Rh62t" 

It should be:

 "..City=Cl&PostalCode=Rh6%200Nt" 

What can I do about this? Thanks in advance!

+4
source share
2 answers

For me it:

 NSString *s=[@"..City=Cl&PostalCode=Rh6 0Nt" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSLog(@"s=%@",s); 

... outputs:

 s=..City=Cl&PostalCode=Rh6%200Nt 

Most likely you are using the wrong encoding.

+31
source

This happens when you try to encode an NSASCIIStringEncoding string with non-ASCII characters.

Make sure you encode the NSUTF8StringEncoding value if the string can contain UTF8 characters or the method returns nil .

0
source

Source: https://habr.com/ru/post/1314193/


All Articles