ios - 在 Swift 中解码字符串

标签 ios swift

如何在 swift 中解码字符串?该字符串现在是 test%2Enl,但正确的格式是 test.nl

我现在有这个:

 let encodedData = encodedString.dataUsingEncoding(NSUTF16StringEncoding)!
 let attributedOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
 let attributedString = NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil, error: nil)!

 return attributedString.string     

最佳答案

您可以使用stringByReplacingPercentEscapesUsingEncoding

var properString = s.stringByReplacingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!

swift 3 更新: 请改用 removingPercentEncoding

关于ios - 在 Swift 中解码字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28310589/

相关文章:

ios - Swift - 由于某种原因,Realm 在主线程上

ios - 在 Xcode 中创建一个新的 Storyboard并使其成为主要 Storyboard(模拟器中出现黑屏)

swift - 以编程方式向 View 添加约束

ios - 实例变量 NSArray 在 Swift 2 - xCode 7.2 中未正确更新

ios - CurrentValueSubject 和 @Published 之间的区别

ios - Xcode 版本 6.1 (6A1030) - Apple Mach O-Linker 错误 - 构建

ios - 收藏 View 未正确显示图像

ios - 如果在 UIScrollView 内,UITableViewController 单元格会消失

ios - 增加 Textfield 的高度时文本不居中?

ios - 如何修复不兼容的指针类型发送?