Swift -- 函数与闭包

  • 参考青玉伏案的博客

参考青玉伏案的博客--函数与闭包

函数

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

    func didClickRightButton(){
        didRightButtonBlock!()
    }
闭包weakself的写法{ [weak self]  参数 in
} 

init(block:()-> Void)
  • 不定参数函数
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关键字修饰形参 可以修改形参的值

  • 默认形参值
/*
    默认形参值
*/
func sayLove(name:String = "凉", loveName:String = "凉") -> String {
    return "\(name)love\(loveName)"
}
sayLove()
sayLove(name: "祝英台", loveName: "梁山伯")
sayLove(name: "他")
sayLove(loveName:"他")
  • 函数类型
//定义两个函数类型相同的函数
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
}

//选择类型的函数并返回相应的函数类型
func choiseCountType(countype:countType) -> ((Int, Int) -> Int) {
    //函数类型变量
    var myFuncType:(Int, Int) -> Int
    
    switch countype {
    case .diffCount:
        myFuncType = diff(number1:number2:)
    case .mulCount:
        myFuncType = mul(number1:number2:)
    }
    return myFuncType
}

//定义相应的函数类型的变量来接收函数中返回的函数类型在调用
var myFuncType:(Int, Int) -> Int
myFuncType = choiseCountType(countype: .diffCount)
myFuncType(20, 10)

myFuncType = choiseCountType(countype: .mulCount)
myFuncType(10, 10)
  • 函数嵌套
func choiseCountType(countType:countType) -> ((Int, Int) ->Int) {
    //定义两个函数类型相同的函数
    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)

闭包

定义一个闭包变量其实就是定义一个特定函数类型的变量(!强制打开闭包)

  • closure 变量的申明
//没有赋初始值的变量,用?表示为可选类型的变量,在使用时用!强制打开
var myClosure0: ((Int, Int) -> int)?

//typealias关键字定义一个特定函数类型
typealias MyClosureType = (Int, Int) -> Int
var myClosure1: MyClosureType?
  • closure变量的赋值
//参数列表跟真正的函数体之间用关键字 in 分隔
myClosure0 = {(num1:Int, num2:Int) -> Int in
    return num1 + num2
}
myClosure0!(10, 50)
  • 闭包回调应用实例
/*
firstVC
*/
//实现回调,接收回调过来的值
    secVC.setbackMyClosure {[weak self](inputText:String) in
      self?.showtextLabel.text = inputText
    }

/*
secondVC
*/
 //定义闭包类型
    typealias InputClosureType = (String) -> Void
    var backClosure:InputClosureType?//接收上个页面传过来的闭包块
//闭包变量的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)
            }
        }
  • 数组中常用的闭包函数
    • Map(映射)

      //对数组中的每一项进行遍历,通过映射对每一项进行处理,处理后的结果以新的数组出现,原来的数组元素保持不变
      var family = [1,2,3,4,5,6]
      var familyMap = family.map {(item: Int) -> String in
          return "我是小\(item)"
      }
      
    • Filter(过滤器)

      //Filter返回新的符合条件的数组
      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 合并数组的items返回合并后的value
      

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

你可能感兴趣的:(Swift -- 函数与闭包)