描述:
在項目開發(fā)中既绩,經(jīng)常會遇到如評論概龄、發(fā)表文章等,可以帶有表情饲握,但是如果后臺不處理上傳的內(nèi)容的話私杜,就會報錯!因此移動端在上傳時救欧,處理一下即可避免報錯衰粹。
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
測試結(jié)果如下:
image.png
image.png