Swift高级开发语言--函数嵌套

Swift高级开发语言--函数嵌套,第1张

概述// 函数嵌套:函数作用域中定义了另外一个函数,内层函数的作用域中可以使用外层函数的参数func helloLanou(var num: Int){ num++ func hello23(){ num++ } hello23() num}helloLanou(10)func hellolanou2() -> ((Int) ->
// 函数嵌套:函数作用域中定义了另外一个函数,内层函数的作用域中可以使用外层函数的参数func hellolanou(var num: Int){    num++    func hello23(){        num++    }    hello23()    num}hellolanou(10)func hellolanou2() -> ((Int) -> String) { func hanshu (i:Int) -> String    {        return "\(i)"    }    return hanshu}hellolanou2()let hanshu = hellolanou2()let i = hanshu(1)// 声明一个函数,实现功能:传入"+","-","*","/"的字符串,返回对应运算的函数:"+"-----返回int + int = intfunc jjcc (str:String) -> ((Int,Int) -> Int)? { func jjcc1 (num1:Int,num2:Int) -> Int {        return num1 + num2    }    func jjcc2 (num1:Int,num2:Int) -> Int {        return num1 - num2    }    func jjcc3 (num1:Int,num2:Int) -> Int {        return num1 * num2    }    func jjcc4 (num1:Int,num2:Int) -> Int {        return num1 / num2    }    switch str {    case "+":        return jjcc1    case "-":        return jjcc2    case "*":        return jjcc3    case "/":        return jjcc4    default:        return nil    }}jjcc("+")let func1 = jjcc("+")let func2 = func1!(1,2)// 函数的返回值是函数,可以用函数嵌套的形式实现,但是并不是必须使用函数嵌套func func2(str:String) -> ((Int,Int) -> Int){ func resultFunc(num1:Int,num2:Int) -> Int{        switch str {        case "+":            return num1 + num2        case "-":            return num1 - num2        case "*":            return num1 * num2        case "/":            return num1 / num2        default:            return 0        }    }    return resultFunc}// 指定类型别名typealias funcType = ((Int,Int) -> Int) func func3 () -> funcType {    func hanshu3 (num1:Int,num2:Int) -> Int {        return  0    }    return hanshu3}
总结

以上是内存溢出为你收集整理的Swift高级开发语言--函数嵌套全部内容,希望文章能够帮你解决Swift高级开发语言--函数嵌套所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1083260.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存