http://blog.csdn.net/lzx_322/article/details/28861199

swift 函数使用前面需要添加 func 有返回值需要使用-> 后面添加返回类型 ,很容易理解,在看英文版的pdf文档时候看到嵌套函数的返回值,刚开始没有太明白,仔细思考了一会还是很容易理解的:

例如:func stepBackward(input: Int) -> Int {
return input - 1
}

返回值是int型的。

func chooseStepFunction(backwards: Bool) -> (Int) -> Int {
func stepForward(input: Int) -> Int { return input + 1 }
func stepBackward(input: Int) -> Int { return input - 1 }
return backwards ? stepBackward : stepForward
}

这里的  chooseStepFunction 返回值类型是(Int) -> Int,看函数中的return 是函数 stepBackward or stepForward,这两个函数的类型正是(Int) -> Int。最终返回的数值是还是int型。

再例如:

func testFunction(a:int,b:int)->(int,int)->int{

func getFunction(a:int,b:int)->int{

return a-b

}

return getFunction

}

外面的函数返回的是(int,int)->int,恰好返回是嵌套里面面的函数,此函数最终返回的是嵌套里面函数的返回值。这是我对嵌套返回的理解,表述不是很清晰,希望对大家有所帮助,不足之处欢迎指出。

版权声明:本文为博主原创文章,未经博主允许不得转载。

swift 之嵌套的理解 func chooseStepFunction(backwards: Bool) -> (Int) -> Int的更多相关文章

  1. swift 闭包+嵌套函数+extension+单例+嵌套函数+??

    //: Playground - noun: a place where people can play import UIKit //*******************嵌套函数********* ...

  2. 如何理解这段代码:void (*signal (int sinno,void(*func)(int)))(int)

    void (*signal (int sinno,void(*func)(int)))(int) 先来看void(*func)(int)   这里的意思是声明一个函数指针func,它的参数类型为int ...

  3. [Swift]LeetCode339. 嵌套链表权重和 $ Nested List Weight Sum

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  4. void (*signal(int sig, void (*func) (int))) (int)理解

    http://blog.csdn.net/sever2012/article/details/8281271 1.signal( int sig, void (*func)(int))signal是一 ...

  5. signal函数理解或者void (*signal(int signum,void(*handler)(int)))(int)理解

    把void (*signal(int signum,void(*handler)(int)))(int)分成两部分: typedef void (*sighandler_t)(int); sighan ...

  6. C# 泛型 Func<object, string, bool> filter

    Func<object, string, bool>是泛型,你可以先把他看成一个普通类型,比如stringpublic class Func{ } // 自定义个普通类. Func fil ...

  7. Swift -> Optional嵌套 探讨

    准备运动:Optional 的介绍 王巍的<Swifter>一书中,介绍了一个有用的命令:在 LLDB 中输入 fr v -R foo,可以查看foo 这个变量的内存构成.我们稍后的分析将 ...

  8. Swift 类型嵌套

    1.类型嵌套 Swift 支持类型嵌套,把需要嵌套的类型的定义写在被嵌套的类型的 {} 中. Swift 中的枚举类型可以辅助实现特定的类或者结构体的功能. struct SchoolUniform ...

  9. Swift - 类型嵌套(以扑克牌结构体为例)

    类型嵌套,简单来说实在一个类型中包含另外一个类型.我们拿一副扑克来说明. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 //类 ...

随机推荐

  1. laravel跨域问题

    // 只有同源策略才允许发送cookies // header('Access-Control-Allow-Credentials:true'); 需要要index.php下开启 最近写登录图形验证码 ...

  2. python学习总结---网络编程

    网络编程 相关概念 - OSI七层模型:它从低到高分别是:物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. - TCP/IP: 在OSI七层模型基础上简化抽象出来的一套网络协议簇,现在得到 ...

  3. Gym101981I Magic Potion(最大流)

    Problem I. Magic Potion There are n heroes and m monsters living in an island. The monsters became v ...

  4. preparedstatement execute()操作成功!但是返回false

    转自http://blog.sina.com.cn/s/blog_963fb3af01013rcs.html Connection con = getConn(); String sql2 = &qu ...

  5. mysql insert into select 语法

    Insert into Table2(field1,field2,...) select value1,value2,... from Table1  这样就对了

  6. 树莓派初次使用的基本配置.md

    记录了一下树莓派初次使用的配置过程,包括装系统.修改 IP 等等. 树莓派(英语:Raspberry Pi),是一款基于 Linux 的单板机电脑. 它由英国的树莓派基金会所开发,目的是以低价硬件及自 ...

  7. Drools 7.4.1.Final参考手册(十四)集成Spring

    集成Spring Drools 6.0重要变更 Drools Spring集成经历了与Drools 6.0的变化完全一致的改造. 以下是一些主要的变化: T*推荐的Drools Spring的前缀已经 ...

  8. 解决Mysql错误Too many connections的方法

    MySQL数据库 Too many connections出现这种错误明显就是 mysql_connect 之后忘记 mysql_close:当大量的connect之后,就会出现Too many co ...

  9. el-upload怎么拿到上传的图片的base64格式

    这里只是本地上传,拿图片的base64,并不向后台直接上传,拿到base64后手动上传 上传前效果: 上传后效果: .vue <el-form-item label="礼品封面&quo ...

  10. mac tomcat: error 1 operation not permitted

    在用微信传一个tomcat压缩包到别人后,解压后无法用idea启动,提示 error 1 operation not permitted,但是直接用命令可以执行. 仔细查看,原来是因为mac出于安全考 ...