对于我正在使用的帖子
var post = "content=\(message)&post=\(time)"
但是当我为变量消息输入类似€的东西时,我收到此错误: 致命错误:在展开Optional值时意外发现nil (lldb)
当没有特殊字符时,它会起作用,我会回来:
2015-02-04 15:38:16.918 sdfff [12306:60b]回复代码:200
2015-02-04 15:38:16.920 sdfff [12306:60b]回复==> { “成功”:1}
答案 0 :(得分:1)
在NSURL中使用特殊字符时,必须使用stringByAddingPercentEscapesUsingEncoding,如下所示:
var post = "content=€&post=€".stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
println(post) // "content=%E2%82%AC&post=%E2%82%AC"
var revert = "content=%E2%82%AC&post=%E2%82%AC".stringByRemovingPercentEncoding!
println(revert) // "content=€&post=€"