首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >swift4迁移后DocumentReadingOptionKey密钥损坏

swift4迁移后DocumentReadingOptionKey密钥损坏
EN

Stack Overflow用户
提问于 2017-09-29 16:19:44
回答 2查看 794关注 0票数 4

使用从swift3迁移到swift4的以下代码,

代码语言:javascript
复制
let options: [NSAttributedString.DocumentReadingOptionKey: AnyHashable] =
                          [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue]

            let str = try NSAttributedString( data:string!.data(using: String.Encoding.utf8, allowLossyConversion: true
                )!, options:options, documentAttributes: nil)
EN

回答 2

Stack Overflow用户

发布于 2017-10-04 16:04:49

我找到了解决方案。您应该删除swift4的.characterEncoding。它在ios8,9,11上工作。

示例:

代码语言:javascript
复制
public func htmlToString() -> String? {
        guard let data = data(using: .utf8) else { return nil }
        do {
            return try NSAttributedString(
                data: data,
                options: [
                    .documentType: NSAttributedString.DocumentType.html
                ],
                documentAttributes: nil
            ).string
        } catch let error as NSError {
            print(error.localizedDescription)
            return  nil
        }
    }

祝你今天愉快!

票数 4
EN

Stack Overflow用户

发布于 2018-05-19 04:48:51

对于所有iOS 8,请使用以下方式作为密钥

NSAttributedString.DocumentReadingOptionKey(rawValue: "CharacterEncoding")

而不是将

NSAttributedString.DocumentReadingOptionKey.characterEncoding

直接了当。

注意:请不要使用版本检查来调用NSAttributedString.DocumentReadingOptionKey.characterEncoding,否则仍然不起作用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46484650

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档