【问题标题】:Debugging closures in Swift with Xcode LLDB console使用 Xcode LLDB 控制台在 Swift 中调试闭包
【发布时间】:2018-08-12 07:40:28
【问题描述】:

我坚持使用 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 命令代替pov 命令的输出格式略有不同,但在大多数情况下它应该显示您需要的信息。

【问题讨论】:

标签: ios swift memory-leaks closures lldb


【解决方案1】:

虽然你可能早就忘记了这个问题,但我的第一个猜测是它是编译器优化,因为你没有在闭包的任何地方使用 response 参数。

@matt 在 cmets 中有更多关于此的内容。

【讨论】:

  • 不,这已经不对了。即使在调试版本中,未使用的值也不在异步闭包中的 LLDB 范围内。
  • 感谢@matt 关注我。我没有密切注意,而且不知道。我把它滚回原来的样子。无论如何,我肯定不对,但这超出了 SO 的范围。 ;)
猜你喜欢
  • 1970-01-01
  • 2015-03-14
  • 1970-01-01
  • 2015-02-17
  • 2011-07-16
  • 2023-03-23
  • 2019-04-26
  • 1970-01-01
  • 2016-02-06
相关资源
最近更新 更多