, . . , , , .
psuedo:
class aaa {
var a = 5;
func foo (b:Int) -> Int {
return a+b;
}
}
now with closing it is the same thing, but you can use local variables in the same way as using a variable defined at the class level
class bbb {
var b = 1;
var closure;
func foo (c:Int) -> Int {
var d = b+c;
closure = (val1:Int) -> Int { Int in
return val1 + b * d;
}
}
func bar () {
b = 3;
closure(5);
}
}
So now, if you run foo, then you can see how closures differ from functions
therefore, the real beauty lies in capturing variables locally, which will be used later, possibly in a completely different area, where, since functions can only use the parameters that you give them, and / or variables defined in the class
source
share