您的当前位置:首页正文

函数与闭包

来源:花图问答

函数和闭包异曲同工

看完函数,再在看闭包时总是觉得相似

定义一个addTwoInts函数,返回a+b
定以一个printMath函数,其中一个参数mathFunction接收一个闭包,函数体内,就执行这个闭包
定义个一个printMathEscape函数,跟上面的printMath函数一样,只不过在global队列执行,就是说这个闭包可能会在函数返回后才执行


func addTwoInts(_ a: Int, _ b: Int) -> Int {
        return a + b
}

func printMath(_ mathFunction:(Int, Int) -> Int, _ a: Int, _ b: Int){
        print("print math result \(mathFunction(a, b))")
}
    
    /**“A closure is said to escape a function when the closure is passed as an argument to the function, but is called after the function returns. ”
    
    一个闭包被称为从一个函数中escape,当这个闭包作为一个参数被传递到那个函数中,但是却在函数返回之后调用。
    
    @escaping 两个条件:
    闭包作为参数
    闭包所在的函数return之后,函数执行
    (编译器其实会提示的)
    
   文档中举得例子,函数中将闭包加在数组中了,延迟调用数组成员
   在这里使用global队列一样的道理
    */
    func printMathEscape(_ mathFunction:@escaping (Int, Int) -> Int, _ a: Int, _ b: Int){
        
        DispatchQueue.global().async {
            print(mathFunction(a,b))
        }
    }

在这里调用上面的函数

常量mathFunc接收一个函数,常量multiply接收一个闭包,option键按住会发现他俩标注的类型是一致的
下面可以更好的理解闭包作为代码块的传递

    func demo2() {
        
        let mathFunc: (Int, Int) -> Int = addTwoInts
        let multiply = { (a: Int, b: Int) -> Int in
            return a * b
        }
        
        
        printMath(mathFunc, 1, 2)//3
        printMath(multiply, 1, 2)//2
        printMath({ (a: Int, b: Int) -> Int in
            return a * b
        }, 2, 2)//4
        printMathEscape({ (a: Int, b: Int) -> Int in
            return a * b
        }, 3, 3)
    }

block和c的函数指针就非常相似,闭包就是为了实现与之相同的功能。那么问题也是一样,开发中还要注意堆栈内存,捕获变量,引用这些问题。