在Swift中解码一个字符串 [英] Decode a string in Swift

查看:81
本文介绍了在Swift中解码一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can i decode a string in swift? The string is now test%2Enl, but the correct format is 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的更新:
使用 removePercentEncoding 而不是。

这篇关于在Swift中解码一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆