Creating a NSAttributedString from HTML


(Félix Cloutier) #1

I'm trying to create a NSAttributedString from its HTML representation in Swift 3 (from the Xcode 8 GM build). However, this doesn't work:

import Foundation
import AppKit

let html = "<i>hello world</i>"
let data = html.data(using: .utf8, allowLossyConversion: true)!
let attributed = NSAttributedString(HTML: data, documentAttributes: nil)

The error that I get in the REPL (which is essentially identical to the one I get in an Xcode project) is:

error: ambiguous use of 'init(HTML:documentAttributes:)'
let str = NSAttributedString(HTML: data!, documentAttributes: nil)
          ^

found this candidate
found this candidate

How should I do it? And perhaps more importantly, what's being done to allow me to figure it out myself?

Félix


(Erica Sadun) #2

I go to a more complex call until the issue goes away. Then file a bug. For me, adding options in fixed the issue.

let attributed = NSAttributedString(html: data, options: [:], documentAttributes: nil)

Swift seems to struggle with these because without "options", the API could match either one.

    public init?(html data: Data, documentAttributes dict: AutoreleasingUnsafeMutablePointer<NSDictionary?>?)

    public init?(html data: Data, options: [AnyHashable : Any] = [:], documentAttributes dict: AutoreleasingUnsafeMutablePointer<NSDictionary?>?)

-- E

···

On Sep 14, 2016, at 9:43 AM, Félix Cloutier via swift-users <swift-users@swift.org> wrote:

I'm trying to create a NSAttributedString from its HTML representation in Swift 3 (from the Xcode 8 GM build). However, this doesn't work:

import Foundation
import AppKit

let html = "<i>hello world</i>"
let data = html.data(using: .utf8, allowLossyConversion: true)!
let attributed = NSAttributedString(HTML: data, documentAttributes: nil)

The error that I get in the REPL (which is essentially identical to the one I get in an Xcode project) is:

error: ambiguous use of 'init(HTML:documentAttributes:)'
let str = NSAttributedString(HTML: data!, documentAttributes: nil)
          ^

found this candidate
found this candidate

How should I do it? And perhaps more importantly, what's being done to allow me to figure it out myself?


(Félix Cloutier) #3

How did you identify the methods? The source location shows as <unknown>:0 here.

Félix

···

Le 15 sept. 2016 à 09:05:06, Erica Sadun <erica@ericasadun.com> a écrit :

On Sep 14, 2016, at 9:43 AM, Félix Cloutier via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

I'm trying to create a NSAttributedString from its HTML representation in Swift 3 (from the Xcode 8 GM build). However, this doesn't work:

import Foundation
import AppKit

let html = "<i>hello world</i>"
let data = html.data(using: .utf8, allowLossyConversion: true)!
let attributed = NSAttributedString(HTML: data, documentAttributes: nil)

The error that I get in the REPL (which is essentially identical to the one I get in an Xcode project) is:

error: ambiguous use of 'init(HTML:documentAttributes:)'
let str = NSAttributedString(HTML: data!, documentAttributes: nil)
          ^

found this candidate
found this candidate

How should I do it? And perhaps more importantly, what's being done to allow me to figure it out myself?

I go to a more complex call until the issue goes away. Then file a bug. For me, adding options in fixed the issue.

let attributed = NSAttributedString(html: data, options: [:], documentAttributes: nil)

Swift seems to struggle with these because without "options", the API could match either one.

    public init?(html data: Data, documentAttributes dict: AutoreleasingUnsafeMutablePointer<NSDictionary?>?)

    public init?(html data: Data, options: [AnyHashable : Any] = [:], documentAttributes dict: AutoreleasingUnsafeMutablePointer<NSDictionary?>?)

-- E