相关疑难解决方法(0)

使用Swift来取消unicode字符,即\ u1234

在使用swift在xcode 6中使用JSON时,我遇到了特殊字符的问题

我在Cocoa/objective C中找到了这些代码来解决转换重音的一些问题但却无法在Swift中运行.有关如何使用它的任何建议?......最好的替代建议也很酷......

谢谢

NSString *input = @"\\u5404\\u500b\\u90fd";
NSString *convertedString = [input mutableCopy];

CFStringRef transform = CFSTR("Any-Hex/Java");
CFStringTransform((__bridge CFMutableStringRef)convertedString, NULL, transform, YES);

NSLog(@"convertedString: %@", convertedString);
// prints: ???, tada!
Run Code Online (Sandbox Code Playgroud)

utf-8 swift

3
推荐指数
3
解决办法
5223
查看次数

如何在Swift中间接显示给定的unicode字符?

在 JSON 数据文件中,我有一个像这样的 unicode 字符:

{
    ”myUnicodeCharacter”: ”\\u{25a1}”
}
Run Code Online (Sandbox Code Playgroud)

我知道如何从 JSON 文件中读取数据。当它包含如上表示的字符时会出现问题。

我将它读入一个字符串变量 myUnicodeCharacterString,它的值是“\u{25a1}”。顺便说一下,我不能在 JSON 数据文件中使用单个斜杠,因为在这种情况下,它无法将文件中的数据识别为正确的 JSON 对象,返回 nil。

然而,当该值被分配给用于显示它的东西时,它不会被编码到它的图形表示中,例如一个 SKLabelNode:

mySKLabelNode.Text = myUnicodeCharacterString // displays ”\u{25a1}” and not a hollow square
Run Code Online (Sandbox Code Playgroud)

问题归结为:

// A: direct approach, does works
let unicodeValueByValue = UnicodeScalar("\u{25a1}") // ”9633”
let c1 = Character(unicodeValueByValue) // ”a hollow square”

// B: indirect approach, this does not work
let myUnicodeString = "\u{25a1}"
let unicodeValueByVariable = UnicodeScalar(myUnicodeString) // Error: cannot find an initialiser
let c2 = Character(unicodeValueByVariable)
Run Code Online (Sandbox Code Playgroud)

那么,如果代码中没有直接给出格式为 …

unicode json swift

2
推荐指数
1
解决办法
1952
查看次数

标签 统计

swift ×2

json ×1

unicode ×1

utf-8 ×1