Mapping Unicode NSString

I want to look at the display of several Unicodes using forLoop. However, the compiler does not like the "% x" or "% d" in the line to create Unicode. Is there any work around?

for (int k = 0; k < 16; k++){ lbl.text =[NSString stringWithFormat:@"\u00B%x", k ];// <-- incomplete universal character name \u00B } 

thanks

0
source share
2 answers

I do not quite understand what you are trying to achieve. For this answer, I assume that you want to generate Unicode characters in the range between B0 and BF.

Your code does not work because of the escape sequence \u (and not because of format specifiers %x or %d ). Just read the error message carefully. The code assumes that the %x specifier will be replaced by the first number and that the escape sequence will be evaluated secondly. However, this happens the other way around: first, the sequence \u is evaluated by the compiler and an error is generated because it is invalid.

A better (and simpler) approach is the following code:

 for (unichar ch = 0xB0; ch <= 0xBF; ch++){ lbl.text =[NSString stringWithFormat:@"%C", ch ]; } 

This code directly puts the Unicode character in a string.

+5
source

Use this method instead:

 NSString stringWithUTF8String: 

From the documentation here in the Non-ASCII Strings and Characters section:

String formatting

+1
source

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


All Articles