2017-01-20 2 views
0

Swift에서 PDF 파서를 작성 중이며 콜백 함수 (CGPDFDictionaryApplyFunction)를 사용하여 모든 글꼴 데이터를 얻었습니다. getFont 함수는 다음과 같습니다. PDFFontCollection 클래스에서 글꼴 사전을 채우기로되어 있습니다. "getFont"콜백 함수 내부에서 콜렉션 변수가 올바르게 채워집니다. 그러나 콜백이 끝나면 글꼴 사전에는 여전히 0 개의 항목이 있습니다.Swift 사전이 C 콜백 함수에서 채우고 싶지 않습니다.

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


} 
+0

3 시간 동안 앉아있어 ... 아직 잘못 생각하지 않았습니다. –

+0

신속한 '사전'이 가치 유형이라고 들었습니까? – OOPer

+0

그렇긴하지만 참조로 전달한다는 것을 지정하지는 않으십니까? –

답변

0

누구든지 관심이있는 분이라면 PeejWeej가 옳습니다. [AnyHashable:Any]은 항상 참조로 전달되기 때문에 fontsNSMutableDictionary으로 선언해야합니다.

관련 문제