Reputation: 471
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
\u2029
, this will give a compiler error and I don't know why.
Upvotes: 0
Views: 1302
Reputation: 5623
Have a look at Strings and Characters Section Special Characters in String Literals.
According to this page \f
is not defined.
The escaped special characters \0 (null character), \ (backslash), \t (horizontal tab), \n (line feed), \r (carriage return), \" (double quote) and \' (single quote)
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
So
\f
Form Feed you may be written in escaped form as \u{000C}
\u2029
Page Feed has to be escaped as \u{2029}
\u2028
Line Separator has to be escaped as \u{2028}
See also "Unicode Control Characters"
Upvotes: 7