使用Swift取消转义Unicode字符,即\ u1234 [英] Using Swift to unescape unicode characters, ie \u1234

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

问题描述

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

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

我在Cocoa/目标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天全站免登陆