Swift -- 函數(shù)與閉包

  • 參考青玉伏案的博客

參考青玉伏案的博客--函數(shù)與閉包

函數(shù)

    var didRightButtonBlock:(()->())?

    func didClickRightButton(){
        didRightButtonBlock!()
    }
閉包weakself的寫法{ [weak self]  參數(shù) in
} 

init(block:()-> Void)
  • 不定參數(shù)函數(shù)
func increatmultableAdd(numbers:Int ...) -> Int {
    var sumOfNumber: Int = 0
    for number in numbers {
        sumOfNumber += number
    }
    return sumOfNumber
}
let sum = increatmultableAdd(numbers: 1,2,3,4,5,6,9,10)
  • 傳參與引用
func incrementStepTwo ( myNumber:inout Int) {
    myNumber += 2
}
var myTestNumber = 6
incrementStepTwo(myNumber: &myTestNumber)

inout關(guān)鍵字修飾形參 可以修改形參的值

  • 默認形參值
/*
    默認形參值
*/
func sayLove(name:String = "涼", loveName:String = "涼") -> String {
    return "\(name)love\(loveName)"
}
sayLove()
sayLove(name: "祝英臺", loveName: "梁山伯")
sayLove(name: "他")
sayLove(loveName:"他")
  • 函數(shù)類型
//定義兩個函數(shù)類型相同的函數(shù)
func diff (number1:Int, number2:Int) -> Int {
    return number1 - number2
}

func mul (number1:Int, number2:Int) -> Int {
    return number1 * number2
}

//定義兩種計算的枚舉類型
enum countType:Int {
    case diffCount = 0
    case mulCount
}

//選擇類型的函數(shù)并返回相應(yīng)的函數(shù)類型
func choiseCountType(countype:countType) -> ((Int, Int) -> Int) {
    //函數(shù)類型變量
    var myFuncType:(Int, Int) -> Int
    
    switch countype {
    case .diffCount:
        myFuncType = diff(number1:number2:)
    case .mulCount:
        myFuncType = mul(number1:number2:)
    }
    return myFuncType
}

//定義相應(yīng)的函數(shù)類型的變量來接收函數(shù)中返回的函數(shù)類型在調(diào)用
var myFuncType:(Int, Int) -> Int
myFuncType = choiseCountType(countype: .diffCount)
myFuncType(20, 10)

myFuncType = choiseCountType(countype: .mulCount)
myFuncType(10, 10)
  • 函數(shù)嵌套
func choiseCountType(countType:countType) -> ((Int, Int) ->Int) {
    //定義兩個函數(shù)類型相同的函數(shù)
    func diff (number1:Int, number2:Int) -> Int {
        return number1 - number2
    }
    
    func mul (number1:Int, number2:Int) -> Int {
        return number1 * number2
    }
    
    var myFuncType:(Int, Int) -> Int
    switch countType {
    case .diffCount:
        myFuncType = diff(number1:number2:)
    case .mulCount:
        myFuncType = mul(number1:number2:)
    default:
        break
    }
    return myFuncType
}
var myFuncType:(Int, Int) -> Int
myFuncType = choiseCountType(countType: .diffCount)
myFuncType(30, 10)
myFuncType = choiseCountType(countType: .mulCount)
myFuncType(30, 10)

閉包

定義一個閉包變量其實就是定義一個特定函數(shù)類型的變量(!強制打開閉包)

  • closure 變量的申明
//沒有賦初始值的變量,用?表示為可選類型的變量,在使用時用!強制打開
var myClosure0: ((Int, Int) -> int)?

//typealias關(guān)鍵字定義一個特定函數(shù)類型
typealias MyClosureType = (Int, Int) -> Int
var myClosure1: MyClosureType?
  • closure變量的賦值
//參數(shù)列表跟真正的函數(shù)體之間用關(guān)鍵字 in 分隔
myClosure0 = {(num1:Int, num2:Int) -> Int in
    return num1 + num2
}
myClosure0!(10, 50)
  • 閉包回調(diào)應(yīng)用實例
/*
firstVC
*/
//實現(xiàn)回調(diào),接收回調(diào)過來的值
    secVC.setbackMyClosure {[weak self](inputText:String) in
      self?.showtextLabel.text = inputText
    }

/*
secondVC
*/
 //定義閉包類型
    typealias InputClosureType = (String) -> Void
    var backClosure:InputClosureType?//接收上個頁面?zhèn)鬟^來的閉包塊
//閉包變量的setter方法
    func setbackMyClosure(tmpClosure:@escaping InputClosureType) {
        backClosure = tmpClosure
    }
    @IBAction func BtnClick(_ sender: UIButton) {
            if backClosure != nil {
                let tmpStr = textFiel.text
                if tmpStr != nil {
                    backClosure!(tmpStr!)//
                }
                navigationController?.popViewController(animated: true)
            }
        }
  • 數(shù)組中常用的閉包函數(shù)
    • Map(映射)

      //對數(shù)組中的每一項進行遍歷,通過映射對每一項進行處理,處理后的結(jié)果以新的數(shù)組出現(xiàn),原來的數(shù)組元素保持不變
      var family = [1,2,3,4,5,6]
      var familyMap = family.map {(item: Int) -> String in
          return "我是小\(item)"
      }
      
    • Filter(過濾器)

      //Filter返回新的符合條件的數(shù)組
      var heightPerson = [170, 188,190,175,168]
      let heightOfFilter = heightPerson.filter{ (height:Int) -> Bool in
          return height >= 173
      }
      heightPerson// [170, 188,190,175,168]
      heightOfFilter//[188, 190, 175]
      
    • Reduce

      //Reduce 合并數(shù)組的items返回合并后的value
      

let salary = [1000, 2000, 3000, 4000, 5000]
let sumSalary = salary.reduce(0) { (sumSalaryTemp:Int, salaryItems:Int) -> Int in
return sumSalaryTemp + salaryItems
}
sumSalary
```

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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