[英]How to convert string to unicode(UTF-8) string in Swift?
如何在 Swift 中將字符串轉換為 unicode(UTF-8) 字符串?
在目標中,我可以這樣寫:
NSString *str = [[NSString alloc] initWithUTF8String:[strToDecode cStringUsingEncoding:NSUTF8StringEncoding]];
如何在 Swift 中做類似的事情?
使用此代碼,
let str = String(UTF8String: strToDecode.cStringUsingEncoding(NSUTF8StringEncoding))
希望它有所幫助
Swift 4我創建了一個String擴展
func utf8DecodedString()-> String {
let data = self.data(using: .utf8)
if let message = String(data: data!, encoding: .nonLossyASCII){
return message
}
return ""
}
func utf8EncodedString()-> String {
let messageData = self.data(using: .nonLossyASCII)
let text = String(data: messageData!, encoding: .utf8)
return text
}
let str = String(describing: strToDecode.cString(using: String.Encoding.utf8))
迅速
let esc_str = str.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.