使用 Swift 对 unicode 字符进行转义,即 u1234 [英] Using Swift to unescape unicode characters, ie u1234

查看:88
本文介绍了使用 Swift 对 unicode 字符进行转义,即 u1234的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 xcode 6 中快速使用 JSON 时遇到特殊字符问题

I have problems with special characters when using JSON in xcode 6 with swift

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

I found these codes in Cocoa/objective C to solve some problems converting accent but could not make it work in Swift. Any suggestions for how to use it? ... best alternative suggestions would also be cool ...

谢谢

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!

推荐答案

它在 Swift 中非常相似,尽管您仍然需要使用 Foundation 字符串类:

It's fairly similar in Swift, though you still need to use the Foundation string classes:

let transform = "Any-Hex/Java"
let input = "\u5404\u500b\u90fd" as NSString
var convertedString = input.mutableCopy() as NSMutableString

CFStringTransform(convertedString, nil, transform as NSString, 1)

println("convertedString: (convertedString)")
// convertedString: 各個都

(最后一个参数让我循环使用,直到我意识到 Swift 中的 Boolean 是 UInt 的类型别名 - 对于这些类型的方法,Objective-C 中的 YES 在 Swift 中变为 1.)

(The last parameter threw me for a loop until I realized that Boolean in Swift is a type alias for UInt - YES in Objective-C becomes 1 in Swift for these types of methods.)

这篇关于使用 Swift 对 unicode 字符进行转义,即 u1234的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆