小编Eth*_*han的帖子

swift如何转换特殊的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 …
Run Code Online (Sandbox Code Playgroud)

unicode swift

-1
推荐指数
1
解决办法
704
查看次数

标签 统计

swift ×1

unicode ×1