Swift語(yǔ)法學(xué)習(xí)

函數(shù)作為參數(shù)

以乘法為例

1.multiply為定義的方法,作為參數(shù)傳入performOperation

@IBAction func operate(sender: UIButton) {
        let operation = sender.currentTitle!
        if userIsInTheMiddleOfTypingNumber {
            enter()
        }
        switch operation{
        case "*": performOperation(multiply)
            
        default:break
        }
    }
    
    func multiply(opt1:Double,opt2:Double)->Double{
        return opt1 * opt2
    }
    
    func performOperation(operation:(Double,Double)->Double){
        if operandStack.count >= 2 {
            displayValue = operation(operandStack.removeLast(), operandStack.removeLast())
            enter()
        }
    }

2.把multiply函數(shù)作為一個(gè)塊,需要改變書(shū)寫(xiě)的語(yǔ)法

    @IBAction func operate(sender: UIButton) {
        let operation = sender.currentTitle!
        if userIsInTheMiddleOfTypingNumber {
            enter()
        }
        switch operation{
        case "*": performOperation({(opt1:Double,opt2:Double)->Double in
            return opt1 * opt2
            })
            
        default:break
        }
    }

3.因?yàn)閟wift可以做到類型推斷,因此可以進(jìn)一步將塊簡(jiǎn)化

    @IBAction func operate(sender: UIButton) {
        let operation = sender.currentTitle!
        if userIsInTheMiddleOfTypingNumber {
            enter()
        }
        switch operation{
        case "*": performOperation({(opt1,opt2) in
            return opt1 * opt2
            })
            
        default:break
        }
    }

4.由于performOperation方法知道調(diào)用的方法將會(huì)返回一個(gè)Double值,而且,此處僅且返回一個(gè)值,因此可以進(jìn)一步簡(jiǎn)化

    @IBAction func operate(sender: UIButton) {
        let operation = sender.currentTitle!
        if userIsInTheMiddleOfTypingNumber {
            enter()
        }
        switch operation{
        case "*": performOperation({(opt1,opt2) in opt1 * opt2})
            
        default:break
        }
    }

5.Swift 不強(qiáng)制要求給參數(shù)命名,如果參數(shù)沒(méi)有命名,默認(rèn)為$0,$1...以此類推,因此,參數(shù)名也可以省略

    @IBAction func operate(sender: UIButton) {
        let operation = sender.currentTitle!
        if userIsInTheMiddleOfTypingNumber {
            enter()
        }
        switch operation{
        case "*": performOperation({$0 * $1})
            
        default:break
        }
    }

6.若此參數(shù)為函數(shù)的最后一個(gè)參數(shù),且為operation類型,則可以將此參數(shù)移到括號(hào)的外面,若之前有其他參數(shù),依然可以放在括號(hào)內(nèi),若此時(shí)除了此參數(shù)沒(méi)有其他參數(shù),則括號(hào)也可以省略,得到最終精簡(jiǎn)版

    @IBAction func operate(sender: UIButton) {
        let operation = sender.currentTitle!
        if userIsInTheMiddleOfTypingNumber {
            enter()
        }
        switch operation{
        case "*": performOperation{$0 * $1}
            
        default:break
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容