Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save PDFDocument to CoreData as NSData (how to convert it to NSData?)

Tags:

ios

swift

pdfkit

so this might be a trivial question, but I can't get it to work. I want to save a pdf file to CoreData after I dropped it onto a view (using IOS' Drag&Drop feature)

func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
        session.loadObjects(ofClass: ComicBookPDFDocument.self) { (pdfItems) in
            let items = pdfItems as! [ComicBookPDFDocument]

            // "Cannot assign value of type 'ComicBookPDFDocument' to type 'NSData?'"
            self.file.data = items[0]
        }
}

ComicBookPDFDocument just subclasses PDFDocument to make it conforming to NSItemProviderReading:

final class ComicBookPDFDocument: PDFDocument, NSItemProviderReading {

    public static var readableTypeIdentifiersForItemProvider: [String] {
        return [kUTTypePDF as String]
    }

    public static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> ComicBookPDFDocument {
        return ComicBookPDFDocument(data: data)!
    }
}

However, I get this compiler error from XCode:

Cannot assign value of type 'ComicBookPDFDocument' to type 'NSData?'

How can I save the pdf data from a PDFDocument? I couldn't find anything on the internet or the documentation.

Thanks for any help

like image 573
Quantm Avatar asked Oct 26 '25 10:10

Quantm


1 Answers

Okay, I don't know how I missed that, but here it is:

items[0].dataRepresentation()
like image 138
Quantm Avatar answered Oct 29 '25 00:10

Quantm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!