Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging closures in Swift with Xcode LLDB console

I stuck with an interesting behavior of Xcode LLDB debug console. When I use weak self + guard self statement for preventing memory leaks then I run into strange behavior while debugging my code when trying to print closure parameter (like the response in the example) or anything class/struct property from closure. It is an example of closure and line with print statement has a breakpoint on which I am trying to print response parameter from Xcode console.

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")
}

I try to use such commands:

po response
p response
print response
e response
expression response

And always I get an error like this:

    (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(     
    ^~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~

Did you stuck with that problem too? Why I get that error and what are possible workarounds except removing weak self + guard self statement or inserting print(response) into the closure code?

Temporary solution:

Don't use self shadowing, instead use variable name something like this _self then you don't get lldb errors like above:

Alamofire.request(url).responseJSON { [weak self] response in
    guard let _self = self else { return }
    // your code here ...
}

Or use v LLDB command instead of po. v command has a bit different output formatting, but it should show you needed info for most cases.

like image 460
m3rk Avatar asked Mar 04 '18 15:03

m3rk


1 Answers

Though you've likely long forgotten about this question, my first guess is that it's a compiler optimization, because you aren't using the response parameter anywhere in the closure.

@matt has a bit more about this in the comments.

like image 168
Clay Bridges Avatar answered Nov 05 '22 21:11

Clay Bridges