Skip to content

Instantly share code, notes, and snippets.

@jonathanduty
Created October 28, 2016 18:27
Show Gist options
  • Select an option

  • Save jonathanduty/0e3ea8361dffc9f24a7d21ef83dfa676 to your computer and use it in GitHub Desktop.

Select an option

Save jonathanduty/0e3ea8361dffc9f24a7d21ef83dfa676 to your computer and use it in GitHub Desktop.
html decode string swift
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