2

I have an HTML string and I want to make an NSAttributedString out of it. This is fairly simple in Objective-C:

NSData* html;
[[NSAttributedString alloc] initWithHTML:html documentAttributes:nil];

This should be fairly simple in Swift as well, but I apparently can't get it right. I have this:

import Foundation
import AppKit

let html: Data = ...
let str = NSAttributedString(HTML: html, documentAttributes: nil)

However, this causes an error:

error: repl.swift:5:11: error: ambiguous use of 'init(HTML:documentAttributes:)'
let str = NSAttributedString(HTML: html, documentAttributes: nil)
          ^

found this candidate
found this candidate

This is from the REPL, but Xcode gives the same error. Of course, "found this candidate" refers to <unknown>:0, so I have no idea where the ambiguity is coming from.

How do I create a NSAttributedString from HTML data in Swift 3.0?

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
zneak
  • 134,922
  • 42
  • 253
  • 328
  • `let str = try? NSAttributedString(data: html, options: [:], documentAttributes: nil)` ? – JAL Sep 15 '16 at 15:47
  • 1
    http://stackoverflow.com/a/28532609/2303865 – Leo Dabus Sep 15 '16 at 15:54
  • http://stackoverflow.com/a/28416599/2303865 – Leo Dabus Sep 15 '16 at 15:55
  • @LeoDabus, are you meaning to say that there is no way to call the documented overload that takes no `options` parameter? – zneak Sep 15 '16 at 16:50
  • @LeoDabus I don't think stackoverflow.com/a/28532609/2303865 and stackoverflow.com/a/28416599/2303865 are a duplicate. They just demonstrate another way of calling `NSAttributedString` that happens to get around this issue. It's a different problem. – Hlung Sep 20 '16 at 08:44
  • @Hlung If you would like me to reopen the question please let me know – Leo Dabus Sep 20 '16 at 08:46
  • @zneak There's another initializer that has options param and it has a default value `[:]` when not given: `init?(html data: Data, options: [AnyHashable : Any] = [:], documentAttributes dict: AutoreleasingUnsafeMutablePointer?)` I think that's the cause. I don't know if there's a way to call the overloaded version. :( – Hlung Sep 20 '16 at 08:53
  • The other thing that annoys me greatly about this problem is that, at least on my machine, swift can't say which overloads are conflicting. – zneak Sep 20 '16 at 10:29

0 Answers0