我正在尝试为 Mac(不是 IOS)制作一个 PDF 查看器来完成一项任务,但我什至不知道如何让 PDF 真正显示出来。我们必须使用 PDFView 和 Quartz。我在这个主题上看到的大多数教程都使用如下内容:
view.setDocument(pdf)
但是 swift 说 PDFView 没有成员 setDocument。我查看了这里的文档,唯一看起来像它可以工作的东西是 setCurrentSelection 所以我尝试了:
import Cocoa
import Quartz
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
@IBOutlet weak var PDFV: PDFView!
func applicationDidFinishLaunching(_ aNotification: Notification) {
let fileURL:URL = (Bundle.main.url(forResource: "example", withExtension: "pdf")! as NSURL) as URL
let pdfDocument:PDFDocument = PDFDocument.self.init(url: fileURL as URL)!
let thing:PDFSelection = PDFSelection.self.init(document: pdfDocument)
PDFV.setCurrentSelection(thing, animate: true)
// Insert code here to initialize your application
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
但这会导致窗口在我运行它时崩溃,并且 xcode 说:线程 1:EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP,子代码 0x0)。有谁知道我实际上打算使用什么?