0

我正在用 Swift 编写 PDF 解析器,并且已经达到了使用回调函数 (CGPDFDictionaryApplyFunction) 获取所有字体数据的地步,getFont 函数应该填充 PDFFontCollection 类中的字体字典。在“getFont”回调函数中,集合变量被正确填充 - 但是当回调完成时,字体字典仍然有 0 个条目。

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!)
    }


}
4

1 回答 1

0

对于可能感兴趣的人,PeejWeej 是对的。您需要声明fonts为 anNSMutableDictionary以便能够填充它,因为[AnyHashable:Any]始终通过引用传递。

于 2018-02-15T12:43:16.530 回答