Swift嵌套函數

來源:互聯網
上載者:User

標籤:swift嵌套函數


    可以將函數定義在其他函數中,成為嵌套函數


    func caculate(opr : String) ->(Int,Int) ->Int {


                func add(a : Int, b : Int) -> Int {

                    return a + b


}

                func sub(a : Int,b : Int) -> Int {

                    return a - b


}


                var result : (Int,Int) -> Int


                switch opr{


                    case "+":

                        result = add

                    case "-":

                        result = sub

                    default: 

                        result = add


}


                  return result


}


        let f1 :(Int,Int)->Int = caculate("+")

         println(f1(1,2))

            

        let f2 :(Int,Int)->Int = caculate("-")


            println(f2(10,2))

        

本文出自 “平凡之路” 部落格,請務必保留此出處http://linjohn.blog.51cto.com/1026193/1620375

Swift嵌套函數

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.