Swift:PDF文档写入
Swift: PDFDocument write
Swift 似乎又变了,我无法使此代码正常工作:
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]))
我收到以下错误:'CFString' 无法转换为 'Any'。
有人知道问题出在哪里吗? API 参考在这里:
https://developer.apple.com/documentation/pdfkit/pdfdocument/1436053-write
与 API 参考中一样,withOptions
参数的类型是 [PDFDocumentWriteOption : Any]
,因此将 diction
声明为 Any
不是一个好主意.
let diction: [PDFDocumentWriteOption : Any] = [kCGPDFContextCreator : value]
有了这行代码,Xcode给了我一个建议:
'CFString' is not implicitly convertible to 'PDFDocumentWriteOption';
did you mean to use 'as' to explicitly convert?
所以,我接受了建议修复:
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!")
//...
}
这段代码编译没有问题。
Swift 似乎又变了,我无法使此代码正常工作:
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]))
我收到以下错误:'CFString' 无法转换为 'Any'。
有人知道问题出在哪里吗? API 参考在这里:
https://developer.apple.com/documentation/pdfkit/pdfdocument/1436053-write
与 API 参考中一样,withOptions
参数的类型是 [PDFDocumentWriteOption : Any]
,因此将 diction
声明为 Any
不是一个好主意.
let diction: [PDFDocumentWriteOption : Any] = [kCGPDFContextCreator : value]
有了这行代码,Xcode给了我一个建议:
'CFString' is not implicitly convertible to 'PDFDocumentWriteOption'; did you mean to use 'as' to explicitly convert?
所以,我接受了建议修复:
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!")
//...
}
这段代码编译没有问题。