我正在进行扩展,将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相同的错误但我无法看到扩展需要什么?
谢谢
NSDocumentTypeDocumentAttribute,NSCharacterEncodingDocumentAttribute,NSHTMLTextDocumentType和
文档属性词典的其他键和值
定义于
AppKit框架(macOS)或UIKit框架(iOS):
文档属性词典的其他键和值
定义于
AppKit框架(macOS)或UIKit框架(iOS):
#if os(macOS) import AppKit #elseif os(iOS) import UIKit #endif