Search code examples
swiftclosurescaptured-variable

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.


Solution

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