在扩展文件中使用未解析的标识符错误?

Use of unresolved identifier error in an extension file?

我正在扩展将 html 转换为属性字符串,代码是

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 ?? ""
}

我遇到以下 3 个相同的错误

Use of unresolved identifier 'NSDocumentTypeDocumentAttribute'

Use of unresolved identifier 'NSHTMLTextDocumentType'

Use of unresolved identifier 'NSCharacterEncodingDocumentAttribute'

我假设我的语法错误导致了 3 个相同的错误,但我看不出扩展还需要什么?

谢谢

NSDocumentTypeDocumentAttributeNSCharacterEncodingDocumentAttributeNSHTMLTextDocumentType 和 文档属性字典的其他键和值 定义在 AppKit 框架 (macOS) 或 UIKit 框架 (iOS):

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