Reputation: 2719
Swift seems to have changed again and I'm having trouble making this code work:
let pdf_url = URL(fileURLWithPath: filename)
let pdf_doc = PDFDocument.init(url: pdf_url)
let value = "Bibbly"
let diction = [kCGPDFContextCreator : value ] as Any
pdf_doc!.write(toFile: filename, withOptions: (diction as [PDFDocumentWriteOption : Any]))
I get the following error: 'CFString' is not convertible to 'Any'.
Anyone know what the problem is? The API reference is here:
https://developer.apple.com/documentation/pdfkit/pdfdocument/1436053-write
Upvotes: 0
Views: 1572
Reputation: 47876
As in the API reference, the type of withOptions
parameter is [PDFDocumentWriteOption : Any]
, so declaring your diction
as Any
is not a good idea.
let diction: [PDFDocumentWriteOption : Any] = [kCGPDFContextCreator : value]
With this line of code, Xcode has given me a suggestion:
'CFString' is not implicitly convertible to 'PDFDocumentWriteOption'; did you mean to use 'as' to explicitly convert?
So, I have Fix-ed it by accepting the suggestion:
let pdf_url = URL(fileURLWithPath: filename)
if let pdf_doc = PDFDocument(url: pdf_url) {
let value = "Bibbly"
let diction: [PDFDocumentWriteOption : Any] = [kCGPDFContextCreator as PDFDocumentWriteOption : value]
pdf_doc.write(toFile: filename, withOptions: diction)
} else {
print("PDF document at: \(filename) cannot be opened!")
//...
}
This code compiles without problems.
Upvotes: 1