objective-c - 如何从整数中生成 unicode 字符?

标签 objective-c cocoa string unicode nsstring

我想制作一个 Unicode 字符数组,但我不知道如何将整数转换为 Unicode 表示形式。这是我到目前为止的代码

NSMutableArray *uniArray = [[NSMutableArray alloc] initWithCapacity:0];
int i;

for (i = 32; i < 300; i++) {
    NSString *uniString = [NSString stringWithFormat:@"\u%04X", i];
    [uniArray addObject:uniString];
}

这给我一个错误“不完整的通用字符名称\u”

有没有更好的方法来构建 Unicode 符号数组?谢谢。

最佳答案

你应该使用 %C 来插入一个 unicode 字符:

NSMutableArray *uniArray = [[NSMutableArray alloc] initWithCapacity:0];
int i;

for (i = 32; i < 300; i++) {
   NSString *uniString = [NSString stringWithFormat:@"%C", i];
   [uniArray addObject:uniString];
}

另一种(更好的?)方法是使用 stringWithCharacters:

NSMutableArray *uniArray = [[NSMutableArray alloc] initWithCapacity:0];
int i;

for (i = 32; i < 300; i++) {
   NSString *uniString = [NSString stringWithCharacters:(unichar *)&i length:1];
   [uniArray addObject:uniString];
}

关于objective-c - 如何从整数中生成 unicode 字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1163373/

相关文章:

iphone - UIScrollView 在 UIView (XIB) 之上并且 scrollview 不会滚动

objective-c - 不从 MainMenu.xib 加载窗口,而是加载另一个窗口

objective-c - EMKey 链问题

objective-c - 如何获取 NSOutlineView 可见行的索引?

cocoa - NSString 字符串扩展不起作用

Java String.getBytes ("UTF8") JavaScript 模拟

string - 在 'eval' 中运行的 Python 字符串

ios - 从今天开始,SKScene 在 spriteKit 中没有改变

ios - 设置 HTTP 方法 : @"POST" not working

python - 如何检查一个单词是否适合指定数量的列?