gpt4 book ai didi

ios - 使用 Xcode LLDB 控制台在 Swift 中调试闭包

转载 作者:行者123 更新时间:2023-12-01 09:58:24 27 4
gpt4 key购买 nike

我坚持使用 Xcode LLDB 调试控制台的一个有趣行为。当我使用 weak self + guard self用于防止内存泄漏的语句然后我在尝试打印闭包参数(如示例中的响应)或闭包中的任何类/结构属性时调试我的代码时遇到了奇怪的行为。这是一个闭包示例,并且带有 print 语句的行有一个断点,我试图在该断点上从 Xcode 控制台打印响应参数。

Alamofire.request(url).responseJSON { [weak self] response in
guard let self = self else { return }
print("This line has a breakpoint and I am trying to print response from debug console")
}
我尝试使用这样的命令:
po response
p response
print response
e response
expression response
我总是收到这样的错误:
    (lldb) po response
error: warning: <EXPR>:12:9: warning: initialization of variable '$__lldb_error_result' was never used; consider replacing with assignment to '_' or removing it
var $__lldb_error_result = __lldb_tmp_error
~~~~^~~~~~~~~~~~~~~~~~~~
_

error: <EXPR>:18:5: error: value of type 'APIManager' has no member '$__lldb_wrapped_expr_25'
$__lldb_injected_self.$__lldb_wrapped_expr_25(
^~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~
你是否也遇到过这个问题?为什么我会收到该错误以及除了删除 weak self + guard self 之外还有哪些可能的解决方法声明或插入 print(response)进入关闭代码?
临时解决方案:
不要使用自阴影,而是使用类似 _self 的变量名那么你不会得到像上面这样的 lldb 错误:
Alamofire.request(url).responseJSON { [weak self] response in
guard let _self = self else { return }
// your code here ...
}
使用 v LLDB 命令而不是 po . v命令的输出格式略有不同,但它应该在大多数情况下显示您需要的信息。

最佳答案

尽管您可能早就忘记了这个问题,但我的第一个猜测是它是编译器优化,因为您没有使用 response闭包中任何位置的参数。
@matt 在评论中有更多关于此的内容。

关于ios - 使用 Xcode LLDB 控制台在 Swift 中调试闭包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49097102/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com