Swift - 函数闭包

原函数:

func operate(sender: UIButton){
    switch operation{
        case "+":
            if operandStack.count >= 2{
                displayValue = operandStack.removeLase() + operandStack.removeLase()
                enter()
            }
        case "-":
            if operandStack.count >= 2{
                displayValue = operandStack.removeLase() - operandStack.removeLase()
                enter()
            }
        case "*":
            if operandStack.count >= 2{
                displayValue = operandStack.removeLase() * operandStack.removeLase()
                enter()
            }
        case "/":
            if operandStack.count >= 2{
                displayValue = operandStack.removeLase() / operandStack.removeLase()
                enter()
            }
        default: break;
    }
}

 

复用函数:

// 加函数
// 参数 , 返回值
func added(op1 double, op2 double) -> double{
    return op1 + op2;
}
func desc(op1 double, op2 double) -> double{
    return op1 - op2;
}
func muti(op1 double, op2 double) -> double{
    return op1 * op2;
}
func sub(op1 double, op2 double) -> double{
    return op1 / op2;
}

// 函数作为参数
// 函数的参数和返回值要和定义的函数相同
func performOperation(operation: (double, double) => double){
    if operandStack.count >= 2 {
        // 具体调用这个参数
        // 相当于一个委托
        displayValue = operation(operandStack.removeLase(),operandStack.removeLase())
        enter()
    }
}

func operate(sender: UIButton){
    switch operation{
        case "+":
            performOperation(added);
        case "-":
            performOperation(desc);
        case "*":
            performOperation(muti);
        case "/":
            performOperation(sub);
        default: break;
    }
}

 

将方法直接定义在调用时
闭包

// 函数作为参数
// 函数的参数和返回值要和定义的函数相同
func performOperation(operation: (double, double) => double){
    if operandStack.count >= 2 {
        // 具体调用这个参数
        // 相当于一个委托
        displayValue = operation(operandStack.removeLase(),operandStack.removeLase())
        enter()
    }
}

func operate(sender: UIButton){
    switch operation{
        case "+":
            // 直接可以吧实现和定义放在调用中
            // 此处成为闭包
            performOperation({(op1 double, op2 double) -> double in
                return op1 + op2;
            });
        case "-":
            performOperation({(op1 double, op2 double) -> double in
                return op1 - op2;
            });
        case "*":
            performOperation({(op1 double, op2 double) -> double in
                return op1 * op2;
            });
        case "/":
            performOperation({(op1 double, op2 double) -> double in
                return op1 / op2;
            });
        default: break;
    }
}

 

 

使用类型推断改进代码
闭包

// 函数作为参数
// 函数的参数和返回值要和定义的函数相同
func performOperation(operation: (double, double) => double){
    if operandStack.count >= 2 {
        // 具体调用这个参数
        // 相当于一个委托
        displayValue = operation(operandStack.removeLase(),operandStack.removeLase())
        enter()
    }
}

func operate(sender: UIButton){
    switch operation{
        case "+":
            // 将 performOperation 中的定义作为该处的类型推断
            // 所以可以不用在这里指明参数和返回值的类型
            performOperation({(op1, op2) in return op1 + op2 })
        case "-":
            performOperation({(op1, op2) in return op1 - op2 })
        case "*":
            performOperation({(op1, op2) in return op1 * op2 })
        case "/":
            performOperation({(op1, op2) in return op1 / op2 })
        default: break;
    }
}

 

当函数中只有一行的时候, 可以不指定 return 语句

// 函数作为参数
// 函数的参数和返回值要和定义的函数相同
func performOperation(operation: (double, double) => double){
    if operandStack.count >= 2 {
        // 具体调用这个参数
        // 相当于一个委托
        displayValue = operation(operandStack.removeLase(),operandStack.removeLase())
        enter()
    }
}

func operate(sender: UIButton){
    switch operation{
        case "+":
            // 只有一行的话 可以不用制定 return 关键字
            performOperation({(op1, op2) in op1 + op2 })
        case "-":
            performOperation({(op1, op2) in op1 - op2 })
        case "*":
            performOperation({(op1, op2) in op1 * op2 })
        case "/":
            performOperation({(op1, op2) in op1 / op2 })
        default: break;
    }
}

 

swift 可以适当省略参数名 用$0, $1... 代替

// 函数作为参数
// 函数的参数和返回值要和定义的函数相同
func performOperation(operation: (double, double) => double){
    if operandStack.count >= 2 {
        // 具体调用这个参数
        // 相当于一个委托
        displayValue = operation(operandStack.removeLase(),operandStack.removeLase())
        enter()
    }
}

func operate(sender: UIButton){
    switch operation{
        case "+":
            // 省略参数名, 使用 $number 代替
            performOperation({ $0 + $1 })
        case "-":
            performOperation({ $0 - $1 })
        case "*":
            performOperation({ $0 * $1 })
        case "/":
            performOperation({ $0 / $1 })
        default: break;
    }
}

 

一个以函数作为参数的 函数, 如果该函数参数是最后一个参数的话, 则可以在调用时移到括号外面

// 函数作为参数
// 函数的参数和返回值要和定义的函数相同
// operation 是最后个参数
func performOperation(operation: (double, double) => double){
    if operandStack.count >= 2 {
        // 具体调用这个参数
        // 相当于一个委托
        displayValue = operation(operandStack.removeLase(),operandStack.removeLase())
        enter()
    }
}

func operate(sender: UIButton){
    switch operation{
        case "+":// 因为 operation 是 performOperation 的最后个函数参数, 所以可以吧实现放到()外面, 并吧()去掉
            performOperation { $0 + $1 }
        case "-":
            performOperation { $0 - $1 }
        case "*":
            performOperation { $0 * $1 }
        case "/":
            performOperation { $0 / $1 } 
        default: break;
    }
}

 

posted @ 2015-12-03 09:26  `Laimic  阅读(149)  评论(0)    收藏  举报