Using an unresolved identifier error in an extension file?

I am doing an extension to convert html to attribute string, code

extension String { var htmlToAttributedString: NSAttributedString? { guard let data = data(using: .utf8) else { return nil } do { return try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil) } catch let error as NSError { print(error.localizedDescription) return nil } } var html2String: String { return htmlToAttributedString?.string ?? "" } 

I get the following 3 identical errors

Using the unresolved identifier 'NSDocumentTypeDocumentAttribute'

Using the unresolved identifier 'NSHTMLTextDocumentType'

Using an Unresolved Identifier 'NSCharacterEncodingDocumentAttribute'

I assume that I made a mistake with the syntax to cause 3 of the same error, but I could not understand what else the extension would need.

thanks

+6
source share
1 answer

NSDocumentTypeDocumentAttribute , NSCharacterEncodingDocumentAttribute , NSHTMLTextDocumentType and other keys and values ​​for document attribute dictionaries are defined in the AppKit framework (macOS) or the UIKit framework (iOS):

 #if os(macOS) import AppKit #elseif os(iOS) import UIKit #endif 
+10
source

Source: https://habr.com/ru/post/1012414/


All Articles