swift 3中的NSAttributedString扩展 [英] NSAttributedString extension in swift 3

查看:316
本文介绍了swift 3中的NSAttributedString扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的代码迁移到swift 3,而我正在努力使用此扩展程序正在使用之前的swift版本。

I'm migrating my code to swift 3 and I'm having a hard time with this extension that was working on the previous swift version.

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

现在当我尝试调用这段代码时,我得到一个像这样的异常错误

Now when I try to call this piece of code I get an exception error like this

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

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

That's how I call the method from my view controller

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


推荐答案

试试这个:

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

官方参考所述,密钥的值 NSCharacterEncodingDocumentAttribute 需要是 NSNumber

As described in the official reference, the value for key NSCharacterEncodingDocumentAttribute needs to be an NSNumber.


NSCharacterEncodingDocumentAttribute

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

The value of this attribute is an NSNumber object containing integer specifying NSStringEncoding for the file;

在较旧的Swifts中, NSStringEncoding 常量导入为 UInt s,因此当转换为 NSNumber 时,它们会自动桥接到 NSNumber AnyObject ,包含在 NSDictionary 中。

In older Swifts, NSStringEncoding constants are imported as UInts, so they are automatically bridged to NSNumber when converted to AnyObject, as contained in NSDictionary.

但是现在,Swift引入了一个新的枚举类型 String.Encoding ,它不是作为Objective-C枚举发起的。不幸的是,现在任何Swift类型都可以包含在 NSDictionary 中,其中间隐藏引用类型为 _SwiftValue ,这绝对不是一个 NSNumber

But now, Swift introduced a new enum type String.Encoding which is not originated as an Objective-C enum. And unfortunately, now any Swift types can be contained in an NSDictionary with intermediate hidden reference type _SwiftValue, which definitely is NOT an NSNumber.

所以,你需要传递一些可以桥接到 NSNumber的东西作为键 NSCharacterEncodingDocumentAttribute 的值。在你的情况下, rawValue 会起作用。

So, you need to pass something which can be bridged to NSNumber as the value for key NSCharacterEncodingDocumentAttribute. In your case, rawValue would work.

在我看来,这应该改进,并且更好地发送一个bug报告 Apple swift.org

In my opinion, this should be improved, and better send a bug report to Apple or swift.org.

这篇关于swift 3中的NSAttributedString扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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