Swift dictionary doesn't want to populate from C Callback function

352 views Asked by At

I'm writing a PDF Parser in Swift and I've reached the point where I get all the font data with a callback function (CGPDFDictionaryApplyFunction), the getFont function is supposed to populate the fonts dictionary in the PDFFontCollection class. Inside the "getFont" callback function the collection variable is correctly populated - however when the callback is finished the fonts dictionary has still 0 entrys.

class PDFFontCollection{
var fonts: [AnyHashable:Any]!

init(page: CGPDFPage){
    fonts = [AnyHashable:Any]()

    let fontsdict = self.findFontDictionary(page: page)

    if(fontsdict != nil){
        CGPDFDictionaryApplyFunction(fontsdict!, self.getFont , &self.fonts)
    }
}




private var getFont: CGPDFDictionaryApplierFunction = { (key, object, info) in
    var collection = info?.assumingMemoryBound(to: [AnyHashable: Any].self).pointee

    var name = String(cString: key, encoding: String.Encoding.ascii)

    var dict: CGPDFDictionaryRef?
    if (CGPDFObjectGetValue(object, .dictionary, &dict)){
        var font = PDFFont.pdfFont(withFontDictionary: dict!)
        collection?.updateValue(font!, forKey: name!)
    }


}
1

There are 1 answers

0
Mihai Popa On

For whoever may be interested, PeejWeej is right. You need to declare fonts as an NSMutableDictionary in order to be able to populate it, since [AnyHashable:Any] is always passed by reference.