【问题标题】:NSAttributedString extension in swift 3Swift 3 中的 NSAttributedString 扩展
【发布时间】:2016-08-31 11:13:32
【问题描述】:

我正在将我的代码迁移到 swift 3,我很难使用这个在以前的 swift 版本上运行的扩展。

extension Data {
    var attributedString: NSAttributedString? {
        do {
            return try NSAttributedString(data: self, options:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8], documentAttributes: nil)
        } catch let error as NSError {
            print(error.localizedDescription)
        }
        return nil
    }
}

现在,当我尝试调用这段代码时,我会收到类似这样的异常错误

error: warning: couldn't get required object pointer (substituting NULL): Couldn't load 'self' because its value couldn't be evaluated

这就是我从视图控制器调用方法的方式

let htmlCode = "<html><head><style type=\"text/css\">@font-face {font-family: Avenir-Roman}body {font-family: Avenir-Roman;font-size:15;margin: 0;padding: 0}</style></head><body bgcolor=\"#FBFBFB\">" + htmlBodyCode + "</body>"
newsDescription.attributedText = htmlCode.utf8Data?.attributedString

【问题讨论】:

    标签: ios swift swift3


    【解决方案1】:

    试试这个:

    extension Data {
        var attributedString: NSAttributedString? {
            do {
                return try NSAttributedString(data: self, options:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)
            } catch let error as NSError {
                print(error.localizedDescription)
            }
            return nil
        }
    }
    

    the official reference 中所述,键NSCharacterEncodingDocumentAttribute 的值必须是NSNumber

    NSCharacterEncodingDocumentAttribute

    此属性的值是一个 NSNumber 对象,其中包含指定 NSStringEncoding 的整数 用于文件;

    在较旧的 Swift 中,NSStringEncoding 常量被导入为 UInts,因此当转换为 AnyObject 时,它们会自动桥接到 NSNumber,如 NSDictionary 中所包含的那样。

    但是现在,Swift 引入了一种新的枚举类型 String.Encoding,它并非起源于 Objective-C 枚举。不幸的是,现在任何 Swift 类型都可以包含在具有中间隐藏引用类型 _SwiftValueNSDictionary 中,这绝对不是 NSNumber

    因此,您需要传递可以桥接到NSNumber 的内容作为键NSCharacterEncodingDocumentAttribute 的值。在你的情况下,rawValue 可以工作。

    在我看来,这应该改进,最好将错误报告发送到Appleswift.org

    【讨论】:

    • 谢谢!但是,由于我使用的是修改后的字体,我不得不再添加一个 catch:catch let error as NSError { print(error.localizedDescription) } catch { print("Description Error") }
    【解决方案2】:

    如果有人在 Swift 5.2+ 中需要帮助:

    extension Data {
        var attributedString: NSAttributedString? {
            do {
                return try NSAttributedString(data: self, options: [ NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue ], documentAttributes: nil)
            } catch let error as NSError {
                print(error.localizedDescription)
            }
            return nil
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-27
      • 2017-01-14
      • 1970-01-01
      相关资源
      最近更新 更多