0

I would like to get all the keys of an CGPDFDictionaryRef. so far this what I found from stackoverflow

Here is my code in swift:

 func printPDFKeys(key: UnsafePointer<Int8>, ob: CGPDFObjectRef, info: UnsafeMutablePointer<()>) -> Void{

 NSLog("key = %s", key);
   //return (key, ob , info)
  }

This function is then called like this.

     let myDoc = CGPDFDocumentCreateWithURL(url)
    if myDoc != nil {
    let myCatalog=CGPDFDocumentGetCatalog(myDoc)

       CGPDFDictionaryApplyFunction(myCatalog, printPDFKeys, nil);//Compiler error here
     }

However I am getting an error that

a c function pointer can only be formed from a reference to a 'func' or a literal closure

I have also tried using a Closure like this:

var printPDFKeys: (  UnsafePointer<Int8>, CGPDFObjectRef,  UnsafeMutablePointer<()> )-> Void

    printPDFKeys={
        key,ob,info in

    NSLog("key = %s", key);

    }

I am still getting the same error. How could I go about it

Community
  • 1
  • 1
user4237435
  • 333
  • 1
  • 2
  • 12
  • Hi, I tried to use your code but it couldn't work for me. Would you please add a complete answer here so it will be helpful to future readers? – Hemang May 31 '17 at 04:42

1 Answers1

1

the correct closure syntax would be:

CGPDFDictionaryApplyFunction(myCatalog, { (key, object, info) -> Void in
// do something
}, nil)
Lennet
  • 413
  • 3
  • 10