无法将NSAttributedString.DocumentAttributeKey类型的值转换为.DocumentReadingOptionKey [英] Cannot convert value of type NSAttributedString.DocumentAttributeKey to .DocumentReadingOptionKey

查看:3022
本文介绍了无法将NSAttributedString.DocumentAttributeKey类型的值转换为.DocumentReadingOptionKey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上找到了这个字符串扩展,允许我将html代码转换为属性字符串:

I found this string extension somewhere on the internet that allows me to turn html code into an attributed string:

func html2AttributedString() -> NSAttributedString {
    return try! NSAttributedString(data: self.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
}

它在Swift 3中运行良好,但是对于Swift 4,Xcode抱怨:

It worked fine in Swift 3, but with Swift 4, Xcode complains:


无法将'NSAttributedString.DocumentAttributeKey'类型的值转换为预期的字典键类型'NSAttributedString.DocumentReadingOptionKey'

Cannot convert value of type 'NSAttributedString.DocumentAttributeKey' to expected dictionary key type 'NSAttributedString.DocumentReadingOptionKey'

如何做我解决了这个问题?

How do I fix this?

推荐答案

你需要传递一个可用的NSAttributedString DocumentType 选项:

You need to pass one of the available NSAttributedString DocumentType options:


超文本标记语言(HTML)文档。

Hypertext Markup Language (HTML) document.



static let html: NSAttributedString.DocumentType








纯文本文件。

Plain text document.



static let plain: NSAttributedString.DocumentType








富文本格式文档。

Rich text format document.



static let rtf: NSAttributedString.DocumentType








包含附件文档的富文本格式。

Rich text format with attachments document.



static let rtfd: NSAttributedString.DocumentType






在这种情况下,您需要传递第一个(html) NSAttributedString.DocumentType.html

所以扩展更新到Swift 4应该如下所示:

So the extension updated to Swift 4 should look like this:

extension String {
    var html2AttributedString: NSAttributedString? {
        do {
            return try NSAttributedString(data: Data(utf8),
                                          options: [.documentType: NSAttributedString.DocumentType.html,
                                                    .characterEncoding: String.Encoding.utf8.rawValue],
                                          documentAttributes: nil)
        } catch {
            print("error: ", error)
            return nil
        }
    }
    var html2String: String {
        return html2AttributedString?.string ?? ""
    }
}




讨论不应该从后台
线程调用HTML导入程序(也就是说,选项字典包含带有
值为html的documentType)。它将尝试与主线程同步,失败,
和超时。从主线程调用它可以工作(但如果HTML包含对外部资源的引用,仍然可以
超时,应该不惜一切代价避免
)。 HTML导入机制意味着
用于实现markdown(即文本样式,
颜色等),而不是一般的HTML导入

Discussion The HTML importer should not be called from a background thread (that is, the options dictionary includes documentType with a value of html). It will try to synchronize with the main thread, fail, and time out. Calling it from the main thread works (but can still time out if the HTML contains references to external resources, which should be avoided at all costs). The HTML import mechanism is meant for implementing something like markdown (that is, text styles, colors, and so on), not for general HTML import

这篇关于无法将NSAttributedString.DocumentAttributeKey类型的值转换为.DocumentReadingOptionKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆