描述:
在项目开发中,经常会遇到如评论、发表文章等,可以带有表情,但是如果后台不处理上传的内容的话,就会报错!因此移动端在上传时,处理一下即可避免报错。
Step1:编码
/// Returns a new string created by replacing all characters in the string
/// not in the specified set with percent encoded characters.
public func addingPercentEncoding(withAllowedCharacters allowedCharacters: CharacterSet) -> String?
Step2:解码
removingPercentEncoding
示例:
// 原文
let contentString = "测试测试一下👍"
print("原文:",contentString)
// 编码:
let contentString1 = contentString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
print("编码后:",contentString1)
2、// 解码:
let contentString2 = contentString.removingPercentEncoding!
print("解码后:",contentString2)
contentLabel.text = "解码之前:\n" + contentString1 + "\n\n解码之后:\n" + contentString2
网友评论