6.3 Swift闭包表达式作为回调函数

        /**

         闭包表达式作为回调函数

         */

        

        /**

         上节课中呢,说了闭包表达式的语法,

         将闭包表达式赋给一个常量并不常用,那种调用方式还不如就写成函数的形式

         

         */

        var array = [20, 2, 3, 70, 8]

        

        showArray(array: array)

        print("-------------->")

        bubbleSort(array: &array)

        

        

        

        

        let intCmp = {

            

            (a: Int, b: Int) -> Int in


            // 可以修改闭包表达式

//            let x = a % 10

//            let y = b % 10

            

            

            if a > b  {

                return -1

            } else if a < b {

                return 1

            } else {

                return 0

            }

            

        }

        

        // 这个就是将闭包作为回调函数了,放到函数形式参数的最后一个,这样方便找

        //

        bubbleSort2(array: &array , cmp: intCmp)

        print("----------------->")

        showArray(array: array)

        

        

        

        

        

        

        

        

        

        

        

        // 好处呢,我们可以修改这个 intCmp

        let intCmp2 = {

            

            (a: Int, b: Int) -> Int in

            

            // 可以修改闭包表达式

            let x = a % 10

            let y = b % 10

            

            

            if x > y  {

                return -1

            } else if x < y {

                return 1

            } else {

                return 0

            }

            

        }

        

        

        // 可以作为实际参数直接放在这里

        bubbleSort2(array: &array , cmp: {

            

            (a: Int, b: Int) -> Int in

            

            // 可以修改闭包表达式

            let x = a % 10

            let y = b % 10

            

            

            if x > y  {

                return -1

            } else if x < y {

                return 1

            } else {

                return 0

            }

        })

        

        print("----------------->")

        showArray(array: array)



   func showArray(array: [Int]) -> Void {

        for x in array {

            print("\(x)")

        }

    }

    

    

    // 写个冒泡排序, 用函数

    func bubbleSort(array:inout [Int]) -> Void {

        

        let cnt = array.count

        

        for (i, value) in array.enumerated()

        {

            if i > 0

            {

                for (j, value2) in array.enumerated()

                {

//                    print("j=====\(j)")

//                    print("====\(cnt - i)")

                    

                    if j < cnt - i

                    {

                        if array[j] > array[j+1]

                        {

                            let t = array[j]

                            array[j] = array[j+1]

                            array[j+1] = t;

                        }

                    }

                }

            }

            

            

        }

        

        

        // 不允许用C的方法了

//        for var i = 1; i < cnt; i++

//        {

//            

//            for var j = 0; j < cnt-i; j++

//            {

//                

//                if array[j] > array[j+1]

//                {

//                    

//                    let t = array[j]

//                    array[j] = array[j + 1]

//                    array[j + 1] = t

//                    

//                }

//                

//            }

//            

//        }

        

        

    }

    

    

    

    // 写个冒泡排序, 用闭包表达式作为回调

    func bubbleSort2(array:inout [Int], cmp:(Int, Int) -> Int) {

        

        let cnt = array.count

        

        for (i, value) in array.enumerated()

        {

            if i > 0

            {

                for (j, value2) in array.enumerated()

                {

                    //                    print("j=====\(j)")

                    //                    print("====\(cnt - i)")

                    

                    if j < cnt - i

                    {

                        if (cmp(array[j], array[j+1]) == -1)

                        {

                            let t = array[j]

                            array[j] = array[j+1]

                            array[j+1] = t;

                        }

                    }

                }

            }

        }

    }


    

    





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值