Created
October 28, 2016 18:27
-
-
Save jonathanduty/0e3ea8361dffc9f24a7d21ef83dfa676 to your computer and use it in GitHub Desktop.
html decode string swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| public extension String { | |
| init(htmlEncodedString: String) { | |
| let encodedData = htmlEncodedString.dataUsingEncoding(NSUTF8StringEncoding)! | |
| let attributedOptions : [String: AnyObject] = [ | |
| NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, | |
| NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding | |
| ] | |
| do { | |
| let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil) | |
| self.init(attributedString.string) | |
| } | |
| catch { | |
| self.init("") | |
| Logger.sharedInstance.error("htmlEncodedString: \(htmlEncodedString)") | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment