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
}
我想将json字符串转换为js脚本,然后调用evaluateJavaScript
,但无法转换特殊字符,如\f
\u2029
,这会产生编译错误,我不知道为什么。
答案 0 :(得分:5)
查看Strings and Characters Section 字符串文字中的特殊字符。
根据此页面\f
未定义。
转义的特殊字符\ 0(空字符),\(反斜杠),\ t (水平标签),\ n(换行),\ r \ n(回车),\“(双 引用)和\'(单引号)
任意Unicode标量,写为 \ u {n},其中n是1-8位十六进制数,其值等于 有效的Unicode代码点
所以
\f
表单Feed您可能以\u{000C}
\u2029
Page Feed必须以\u{2029}
\u2028
行分隔符必须以\u{2028}