简体   繁体   English

是否可以检查在 Swift 中作为参数传递的闭包的内容?

[英]Is it possible to inspect the contents of a closure passed as a parameter in Swift?

Working with Xcode 13.2.1 and Swift 5.X使用 Xcode 13.2.1 和 Swift 5.X

I have a problem with a function that receives a closure as parameter.我对接收闭包作为参数的 function 有疑问。 The code has been working fine and now that I implemented new code calling the same function with a different closure (from a different point), when it is executed, it does not do anything (as if the closure is empty).代码一直运行良好,现在我实现了使用不同闭包(从不同点)调用相同 function 的新代码,当它被执行时,它不做任何事情(好像闭包是空的)。 So I want to debug or inspect what is inside the received closure, but I can't find a way to do it.所以我想调试或检查接收到的闭包中的内容,但我找不到办法。

I added a simplified example to show what I want to accomplish我添加了一个简化的例子来展示我想要完成的事情

var closureA = {
    // code here
    print("Closure A")
}

var closureB = {
    // code here
    print("Closure B")
}

func callWithClosure(closure: (()->Void)? = nil) {
    // code
    print("Closure being passed: \(String(describing: closure))")
    closure.debugDescription
    closure?()
}

callWithClosure(closure: closureA)

The printout is:打印输出是:

Closure being passed: Optional((Function))
Closure A

If I add a breakpoint and try to use po closure I also only see Optional(Function)如果我添加断点并尝试使用po closure ,我也只会看到Optional(Function)

Is this a limitation of the LLVM or Xcode?这是 LLVM 或 Xcode 的限制吗?

....e ....e

So I want to debug or inspect what is inside the received closure, but I can't find a way to do it.所以我想调试或检查接收到的闭包中的内容,但我找不到办法。

There's no way to print the source code of an arbitrary closure from inside your program.无法从程序内部打印任意闭包的源代码。

That said, you can use the debugger to see the code.也就是说,您可以使用调试器查看代码。 Set a breakpoint on the closure() call or in the closure you're interested in and debug the program.closure()调用或您感兴趣的闭包中设置断点并调试程序。 Execution will stop at the breakpoint, just as with any other code, and you can step through the closure's code.执行将在断点处停止,就像任何其他代码一样,您可以单步执行闭包的代码。 You can also look at the stack trace to see how you reached that point in the code, and you can inspect the calling code to see what closure is being passed in.您还可以查看堆栈跟踪以查看您是如何到达代码中的那个点的,并且您可以检查调用代码以查看正在传递的闭包。

If I add a breakpoint and try to use po closure I also only see Optional(Function)如果我添加断点并尝试使用 po 闭包,我也只会看到 Optional(Function)

Debugging into a closure works because the debugger knows how to connect the object code to the source code that it was compiled from.调试闭包是有效的,因为调试器知道如何将 object 代码连接到编译它的源代码。 But at run time, all the source code has already been compiled -- the contents of the closure itself is object code, not source code that you can inspect.但是在运行时,所有的源代码都已经被编译——闭包本身的内容是 object 代码,而不是您可以检查的源代码。 It's a limitation neither of Xcode nor of LLVM -- it's just how software works in a compiled environment.这既不是 Xcode 的限制,也不是 LLVM 的限制——它只是软件在编译环境中的工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM