NSString Unicode显示 [英] NSString Unicode display
问题描述
我想使用forLoop来查看一些Unicodes的显示。但是编译器不喜欢字符串中的%x或%d来构建Unicode。是否有工作?
for(int k = 0; k <16; k ++){
pre>
lbl .text = [NSString stringWithFormat:@\u00B%x,k]; //< - 不完全通用字符名称\u00B
}
感谢
解决方案你想达到的目标。对于这个答案,我假设你想生成的B0和BF之间的范围内的Unicode字符。
您的代码不工作,由于
\u
转义序列(而不是因为%x
或%d
格式说明符)。只是仔细阅读错误消息。该代码假定%x
说明符将首先被替换为一个数字,并且将首先计算转义序列。但是,它发生的另一个方法:首先\u
序列由编译器评估,并抛出一个错误,因为它是无效的。
一个更好(和更简单)的方法是下面的代码:
for(unichar ch = 0xB0; ch <= 0xBF; ch ++){
lbl.text = [NSString stringWithFormat:@%C,ch];
}
此代码直接将Unicode字符放入字符串。
I want to look at the display of a number of Unicodes using a forLoop. However the compiler doesn't like "%x" or "%d" in the string to build a Unicode. Is there a work around?
for (int k = 0; k < 16; k++){ lbl.text =[NSString stringWithFormat:@"\u00B%x", k ];// <-- incomplete universal character name \u00B }
thanks
解决方案I'm not sure a fully understand what you're trying to achieve. For this answer, I assume that you want to generate the Unicode characters int the range between B0 and BF.
Your code doesn't work due to the
\u
escape sequence (and not because of the%x
or%d
format specifiers). Just read the error message carefully. The code assumes that the%x
specifier will be substituted with a number first and that the escape sequence will be evaluated second. However, it happens the other way round: First the\u
sequence is evaluated by the compiler and an error thrown 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 a Unicode character into the string.
这篇关于NSString Unicode显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!