2

I am trying to implement an app where I would like to show some text in Spanish format. For example I would like to show "España" but in my label it shows "Espa√ɬ±a" and also it changes the text for some of other text.

How to get rid of these. If anybody could help. Thanks.

Edit: When i am getting my response it logs that Below result

Message = ( "Espa\U221a\U00c9\U00ac\U00b1a:1.3\U221a\U00c7\U00ac\U00a2/min" );

But when i extract the value according to key from Dictionary it shows

España:1.3¢/min

It means when i am getting the value from dictionary it cant do proper decoding.

how to resolve this. Any idea..?

Tanvir Nayem
  • 702
  • 10
  • 25
  • "España" displays on my label. Please add your code. – trungduc May 17 '18 at 07:33
  • it is working, please share your code – PPL May 17 '18 at 07:33
  • "España" directly is showing in my label. But when it comes from response it is not showing properly. Is there any way to parse HTML unicode to iOS unicode.? – Tanvir Nayem May 17 '18 at 07:36
  • @Tann Add the code where you parse response to get text – trungduc May 17 '18 at 07:41
  • I am getting the responser as a dictionary. here is my code => NSString *message = [[[[response objectForKey:@"key1"]objectAtIndex:0] objectForKey:@"key2"] objectAtIndex:0]; In this message string i am getting the value. – Tanvir Nayem May 17 '18 at 07:45
  • Possible duplicate of [How to convert string to unicode(UTF-8) string in Swift?](https://stackoverflow.com/questions/37087325/how-to-convert-string-to-unicodeutf-8-string-in-swift) – Tamás Sengel May 17 '18 at 07:50

1 Answers1

0

First convert your response String to NSData using NSUTF8StringEncoding encoding, then again convert the same data to finalString like below.

NSString *string = @"España"; //Your response String goes here
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSString *finalString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
lblTemp.text = finalString;

UPDATE 1

I think there is some error from your response, Please see below

NSString *string = @"Nu\\u0161a Florjan\\u010di\\u010d";
NSString *finalString = [NSString
                   stringWithCString:[string cStringUsingEncoding:NSUTF8StringEncoding]
                   encoding:NSNonLossyASCIIStringEncoding];
NSLog(@"finalString = %@", finalString);

Output of above code is,

finalString = Nuša Florjančič

UPDATE 2

If you want output string like "España", your desired response should be "Espa\u00F1a", Find below,

NSString *string = @"Espa\\u00F1a";
NSString *finalString = [NSString
                         stringWithCString:[string cStringUsingEncoding:NSUTF8StringEncoding]
                         encoding:NSNonLossyASCIIStringEncoding];
NSLog(@"%@",finalString);

Output is España

PPL
  • 6,357
  • 1
  • 11
  • 30