Swift Code:
func makeIncrementor(forIncrement amount: Int) -> (() -> Int,() -> Int) { var runningTotal = func incrementor() -> Int { runningTotal += amount return runningTotal } func decrementor()->Int{ runningTotal -= amount return runningTotal } runningTotal += ; return (incrementor,decrementor) } let counter = makeIncrementor(forIncrement: ); let a1 = counter.(); assert(a1==, "a1") let a2 = counter.(); assert(a2==, "a2") let a3 = counter.(); assert(a3==, "a3") let c1 = counter.(); assert(c1==, "c1")
Objective-C Code:
__block int runningTotal = ; int (^incrementor)() = ^(){
return ++runningTotal;
}; int (^decrementor)() = ^(){
return --runningTotal;
};
runningTotal=;
int a1 = incrementor();
int a2 = incrementor();
int a3 = incrementor();
int c1 = decrementor();