[英]swift how to convert special unicode
let result = ["response": response,
"callbackId": callbackId]
do {
let data = try NSJSONSerialization.dataWithJSONObject(result, options: .PrettyPrinted)
var str = NSString(data: data, encoding: NSUTF8StringEncoding) as? String
str = str?.stringByReplacingOccurrencesOfString("\\", withString: "\\\\")
str = str?.stringByReplacingOccurrencesOfString("\"", withString: "\\\"")
str = str?.stringByReplacingOccurrencesOfString("\'", withString: "\\\'")
str = str?.stringByReplacingOccurrencesOfString("\n", withString: "\\n")
str = str?.stringByReplacingOccurrencesOfString("\r", withString: "\\r")
// str = str?.stringByReplacingOccurrencesOfString("\f", withString: "\\f")
// str = str?.stringByReplacingOccurrencesOfString("\u2028", withString: "\\u2028")
// str = str?.stringByReplacingOccurrencesOfString("\u2029", withString: "\\u2029")
return "bridge.invokeJs('{\"response\" : {\"username\" : \"zhongan\"},\"callbackId\" : \(callbackId)}')"
} catch {
return nil
}
I want to convert the json string to js script, and then call evaluateJavaScript
, but can not convert the special character, like \\f
\
, this will give a compiler error and I don't know why. 我想JSON字符串转换成js脚本,然后调用
evaluateJavaScript
,但不能转换的特殊字符,如\\f
\
,这会给出一个编译器错误,我不知道为什么。
Have a look at Strings and Characters Section Special Characters in String Literals . 请看一下字符串文字中的字符串和字符部分的特殊字符 。
According to this page \\f
is not defined. 根据此页面,未定义
\\f
。
The escaped special characters \\0 (null character), \\ (backslash), \\t (horizontal tab), \\n (line feed), \\r (carriage return), \\" (double quote) and \\' (single quote)
转义的特殊字符\\ 0(空字符),\\(反斜杠),\\ t(水平制表符),\\ n(换行符),\\ r(回车),\\“(双引号)和\\'(单引号)
An arbitrary Unicode scalar, written as \\u{n}, where n is a 1–8 digit hexadecimal number with a value equal to a valid Unicode code point
任意Unicode标量,写为\\ u {n},其中n是1–8位数的十六进制数字,其值等于有效的Unicode代码点
So 所以
\\f
Form Feed you may be written in escaped form as \\u{000C}
\\f
\\u{000C}
可能会以\\u{000C}
形式转义 \
Page Feed has to be escaped as \\u{2029}
\
必须将页面Feed转义为\\u{2029}
\
Line Separator has to be escaped as \\u{2028}
\
行分隔符必须转为\\u{2028}
See also " Unicode Control Characters " 另请参阅“ Unicode控制字符 ”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.