inf*_*ouk 7 nsattributedstring swift swift3
我正在进行扩展,将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 ?? ""
}
Run Code Online (Sandbox Code Playgroud)
我收到以下3个相同的错误
使用未解析的标识符'NSDocumentTypeDocumentAttribute'
使用未解析的标识符'NSHTMLTextDocumentType'
使用未解析的标识符'NSCharacterEncodingDocumentAttribute'
我假设我犯了一个错误的语法导致3相同的错误但我无法看到扩展需要什么?
谢谢
Mar*_*n R 13
NSDocumentTypeDocumentAttribute,NSCharacterEncodingDocumentAttribute,NSHTMLTextDocumentType和用于文件等的键和值属性词典都是AppKit框架(MACOS)或在UIKit框架(IOS)定义为:
#if os(macOS)
import AppKit
#elseif os(iOS)
import UIKit
#endif
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4431 次 |
| 最近记录: |