benwiggy
benwiggy

Reputation: 2749

Swift: "Ambiguous reference to member" init

There are several posts on this subject, but I can't see the answer to this problem in them. The following lines produce the title error:

let pdf_url = NSURL.fileURL(withPath: inputfile)
let pdf_doc = PDFDocument.init?(url: pdf_url) as! URL

I understand that the init method can take a variety of data types, but surely I've made it pretty clear that I want a URL.

Error message is:

error: ambiguous reference to member 'init(url:)'

A problem which may be related is that Xcode occasionally tells me to use URL, not NSURL, in the first line, but then complains that URL doesn't have the fileURL method. (I also tried CGPDFDocument, but that wanted a CFURL, and wouldn't accept NSURL, even though they are supposed to be "toll-free bridged".)

Upvotes: 0

Views: 1999

Answers (2)

vadian
vadian

Reputation: 285290

The error occurs because you (try to) cast PDFDocument to URL.

You probably mean

PDFDocument.init?(url: pdf_url as! URL) 

But the recommended Swift 3 syntax is

let pdfURL = URL(fileURLWithPath: inputfile) // returns a non-optional URL
let pdfDoc = PDFDocument(url: pdfURL)

Please use – Swift – camel case variable names rather than – javascript / php – snake case.

Upvotes: 1

Jean-Baptiste Yunès
Jean-Baptiste Yunès

Reputation: 36441

Use URL:

let pdf_url = URL(fileURLWithPath: inputfile)

Upvotes: 1

Related Questions