6

The question is different because I want to take out info value out of the closure. And right now, it's empty thus I couldn't call any outside function. This is what I have tried so far:

CGPDFOperatorTableSetCallback(operatorTableRef!, "BT") { (scanner, info) in
    if let info = info {
        let myObject = Unmanaged<ViewController>.fromOpaque(info).takeUnretainedValue()
        myObject.printinfo(info: info as AnyObject)
}
    }

func printinfo(info:AnyObject) -> Void {
    print("Printing:\n\(info)")
    print("------------------------------------------------------------")
}

I am playing with a PDF to extract strings from it. There's one call block that I should implement. All well, except I can't call any outside function or can't use any variable within that block.

    CGPDFOperatorTableSetCallback(operatorTableRef!, "BT") { (scanner, info) in
        //I can't do anything here except this:
        print("Yes, I can print.")
        //hello() //or self.hello() //all cries.
}

func hello()->Void {
    //hello.
}
Hemang
  • 26,840
  • 19
  • 119
  • 186
  • 1
    This *is* a duplicate of [How to use instance method as callback for function which takes only func or literal closure](https://stackoverflow.com/questions/33260808/how-to-use-instance-method-as-callback-for-function-which-takes-only-func-or-lit), even if you reopened it: Same error message ("A C function pointer cannot be formed from a closure that captures context"), same problem (you cannot use "self" in the closure which is used as C callback), same solution (bridge "self" to void pointer and back). – Martin R May 30 '17 at 12:43
  • Possible duplicate of [How to use instance method as callback for function which takes only func or literal closure](https://stackoverflow.com/questions/33260808/how-to-use-instance-method-as-callback-for-function-which-takes-only-func-or-lit) – Larme May 30 '17 at 12:58
  • @Larme, yes it can be but my case it different, as you can see in the updated question. – Hemang May 31 '17 at 04:35
  • @Hemang: What does not work with your updated code? How do you call CGPDFScannerCreate? – Martin R May 31 '17 at 05:06
  • @MartinR, here I want to confirm: I am able to get a call inside the closure but then `if let info = info {...}` condition never matches. – Hemang May 31 '17 at 05:14
  • 1
    @Hemang: You have to pass "self" (converted to a UnsafeMutableRawPointer) to CGPDFScannerCreate (as in the answer that I linked to). – Martin R May 31 '17 at 05:16
  • I am not understanding what you mean, could you please add your answer here? – Hemang May 31 '17 at 05:20
  • 1
    You do `let scanner = CGPDFScannerCreate(stream, operatorTableRef, nil)`, right? Ask you this: WHY do you write "nil" in the third parameter? What's the name of that parameter in the declaration of that method? If you put (a valid) let's call it `MyCustomValidVard, then `CGPDFOperatorTableSetCallback(operatorTableRef!, "BT") { (scanner, info) in` `info` will be `MyCustomValidVard`. So if you give `self` using the linked answer, you'll be able to use `self` in that closure. – Larme May 31 '17 at 06:18

0 Answers0