加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

DocumentReadingOptionKey键在swift4迁移后损坏

发布时间:2020-12-14 04:38:36 所属栏目:百科 来源:网络整理
导读:使用以下代码从 swift3迁移到 swift4, let options: [NSAttributedString.DocumentReadingOptionKey: AnyHashable] = [.documentType: NSAttributedString.DocumentType.html,.characterEncoding: String.Encoding.utf8.rawValue] let str = try NSAttribute
使用以下代码从 swift3迁移到 swift4,

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)

iOS 9没有问题,运行iOS 8.3时,控制台输出:“dyld:找不到符号:_NSCharacterEncodingDocumentOption”;它将在评论“.characterEncoding:String.Encoding.utf8.rawValue”之后传递.

解决方法

我找到了解决方案.你应该为swift4删除.characterEncoding.
它适用于ios8,9,11.

例:

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

祝你有美好的一天!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读