in swift, local variables in closure

Viewed 848

I'm studying Swift closures but I have some questions about closure capture lists.

I know that closures are a reference type. I know the meaning of reference type and value type.

But in a closure, when they capture the values (local variables of closures), how can it be that local variables are reference type?

For example:

var i = 0
var closureArrays:[()->()] = []
for _ in 1...5{
    closureArrays.append{
        print(i)
    }
    i += 1
}

I think this is the most famous example of capture list in closure.

Then I thought i is the integer type and it is also value type but how can i be the reference type in closure blocks?

Is this the same concept with scope chain in javascript?

Can anyone explain clearly?

0 Answers
Related