Sometimes this thing crashes but I don't know why and when. Does someone have an idea?
extension String {
var htmlDecoded: String? {
if let encodedData = self.data(using: String.Encoding.utf8) as Data? {
let attributedOptions = [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue] as [String : Any]
do {
let attributedString = try NSAttributedString(data: encodedData,
options: attributedOptions,
documentAttributes: nil)
return attributedString.string
} catch let error as NSError {
print("ERROR: ", error.localizedDescription)
return self
}
}
return self
}
}
This is the error I get from HockeyApp
function signature specialization <Arg[0] = Owned To Guaranteed and Exploded> of @nonobjc (extension in UIKit):__C.NSAttributedString.init(data: Foundation.Data, options: [Swift.String : Any], documentAttributes: Swift.AutoreleasingUnsafeMutablePointer<__C.NSDictionary?>?) throws -> __C.NSAttributedString (String+html.swift:0)
function signature specialization <Arg[0] = Exploded> of (extension in Podcat_2):Swift.String.htmlDecoded.getter : Swift.String? (String+html.swift:0)
I would strongly recommand you to upgrade to swift 4. Font are changed a lot in swift 4 and it's better to make the change earlier.
Here is my swift 4 version that works great for me to convert html string to NSAttributedString
. You can then call .string
to get the string itself.
extension String {
func htmlAttributedString() -> NSAttributedString? {
guard let data = self.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return nil }
guard let html = try? NSMutableAttributedString(
data: data,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil) else { return nil }
return html
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With