Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift capture value with same name

In a closure can we use the same name somehow inside as well as a value that is captured by the closure.

func load(withResource resource: Resource) {
    var data: A?
    var error: Error?

    load(resource: resource) { (result, error) in
        data = result // Ok!
        error = error // error!
    }

    print("data: \(data), error: \(error)")
}

I am thinking if there is something like using self if we were talking about stored properties but these vars are are declared in the function scope.

The easiest way would just to rename error but I was wondering if there is another way.

like image 308
richy Avatar asked Apr 07 '26 07:04

richy


1 Answers

First, If your load method is asynchronous, error and data will always be nil when it's printed.

The direct answer to your question is that within the scope of the closure, the value of "error" is the value from the parameter and there's no way to access the error from the function.

Obviously there are a lot of options to get around this, but one clean option would be to make your information into a Tuple:

func load(withResource resource: Resource) {

    var closureData: (data: A?, error: Error?)?

    load(resource: resource) { (result, error) in
       closureData = (result, error)
    }

    //print(closureData) if you don't need to format it)
    print("data: \(closureData.data), error: \(closureData.error)")
}
like image 120
GetSwifty Avatar answered Apr 10 '26 00:04

GetSwifty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!