小编Ram*_*los的帖子

swift 3中的NSAttributedString扩展

我正在将我的代码迁移到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
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

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
Run Code Online (Sandbox Code Playgroud)

ios swift swift3

14
推荐指数
2
解决办法
1万
查看次数

你如何覆盖swift 3中的layerClass?

我试图layerClass()在swift 3中覆盖该方法,但我收到了错误Method does not override any method from its superclass.在swift 2.x中,我这样做,是否有人知道新语法?

override func layerClass() -> AnyClass { return CAGradientLayer.self }
Run Code Online (Sandbox Code Playgroud)

swift3

10
推荐指数
1
解决办法
5324
查看次数

标签 统计

swift3 ×2

ios ×1

swift ×1