微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

DocumentReadingOptionKey键在swift4迁移后损坏

使用以下代码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
        }
    }

祝你有美好的一天!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐