Swift与Objective-C中的闭包

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();
上一篇:Swift调用Objective C的FrameWork


下一篇:夺命雷公狗---node.js---19之项目的构建在node+express+mongo的博客项目4mongodb在项目中的基本引入