一、函数类型

Every function in Swift has a type, consisting of the function’s parameter types and return type.

Function Types

Every function has a specific function type, made up of the parameter types and the return type of the function.

For example:

  1. func addTwoInts(_ a: Int, _ b: Int) -> Int {
  2. return a + b
  3. }
  4. func multiplyTwoInts(_ a: Int, _ b: Int) -> Int {
  5. return a * b
  6. }

This example defines two simple mathematical functions called addTwoInts and multiplyTwoInts. These functions each take two Int values, and return an Int value, which is the result of performing an appropriate mathematical operation.

The type of both of these functions is (Int, Int) -> Int. This can be read as:

“A function that has two parameters, both of type Int, and that returns a value of type Int.”

Using Function Types

You use function types just like any other types in Swift. For example, you can define a constant or variable to be of a function type and assign an appropriate function to that variable:

  1. var mathFunction: (Int, Int) -> Int = addTwoInts

This can be read as:

“Define a variable called mathFunction, which has a type of ‘a function that takes two Int values, and returns an Int value.’ Set this new variable to refer to the function called addTwoInts.”

Function Types as Parameter Types

Here’s an example to print the results of the math functions from above:

  1. func printMathResult(_ mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {
  2. print("Result: \(mathFunction(a, b))")
  3. }
  4. printMathResult(addTwoInts, 3, 5)
  5. // Prints "Result: 8"

Function Types as Return Types

右侧结合律

Function Argument Labels and Parameter Names

Each function parameter has both an argument label and a parameter name. The argument label is used when calling the function; each argument is written in the function call with its argument label before it. The parameter name is used in the implementation of the function. By default, parameters use their parameter name as their argument label.

Specifying Argument Labels

  1. func greet(person: String, from hometown: String) -> String {
  2. return "Hello \(person)! Glad you could visit from \(hometown)."
  3. }
  4. print(greet(person: "Bill", from: "Cupertino"))

关键字:

In-Out Parameters

Function parameters are constants by default. Trying to change the value of a function parameter from within the body of that function results in a compile-time error.

Optional Tuple Return Types

An optional tuple type such as (Int, Int)? is different from a tuple that contains optional types such as (Int?, Int?). With an optional tuple type, the entire tuple is optional, not just each individual value within the tuple.

func minMax(array: [Int]) -> (min: Int, max: Int)?

返回值需要判断

swift语言点评五-Function的更多相关文章

  1. Swift语言指南(五)--数字字面量和数字类型转换

    原文:Swift语言指南(五)--数字字面量和数字类型转换 数字字面量 整数字面量写法如下: · 十进制数,无前缀 · 二进制数,以 0b 为前缀 · 八进制数,以 0o 为前缀 · 十六进制数,以 ...

  2. swift语言点评十五-$0

    import UIKitimport XCPlayground class ViewController: UIViewController { func action() { print(" ...

  3. swift语言点评四-Closure

    总结:整个Closure的作用在于简化语言表述形式. 一.闭包的简化 Closure expression syntax has the following general form: { () -& ...

  4. swift语言点评一

    一.变量定义 1.常量与变量 Use let to make a constant and var to make a variable. 2.类型与推测 However, you don’t alw ...

  5. swift语言点评十九-类型转化与检查

    1.oc比较: -(BOOL) isKindOfClass: classObj判断是否是这个类或者这个类的子类的实例 -(BOOL) isMemberOfClass: classObj 判断是否是这个 ...

  6. swift语言点评十八-异常与错误

    1.错误类型与枚举的结合 enum VendingMachineError: Error { case invalidSelection case insufficientFunds(coinsNee ...

  7. swift语言点评二

    一.数据类型 1.基础类型的封装 Swift provides its own versions of all fundamental C and Objective-C types, includi ...

  8. swift语言点评十七-Designated Initializers and Convenience Initializers

    Swift defines two kinds of initializers for class types to help ensure all stored properties receive ...

  9. swift语言点评十六-Initialization && Deinitialization

    initial value:必须初始化.不影响观察者 Classes and structures must set all of their stored properties to an appr ...

随机推荐

  1. Hessian 接口使用示例总结

    一.使用hessian接口准备 首先,hessian接口的使用,必须要准备hessian接口的jar包,本文使用的jar包如下:hessian-4.0.7.jar; Hessian接口的使用一般是在两 ...

  2. ​libz.so.1: cannot open shared object file: No such file or directory

    在虚拟机安装xtrabackup工具时候出现的报错:​​libz.so.1: cannot open shared object file: No such file or directory [ro ...

  3. BAT三家互联网公司哪家更注重用户体验?

    这几天百度的用户体验又成了设计圈关注的对象,李彦宏好不容易刷出来的好感度一下子被打入了冰点,通过此次事件,不难看出现在的互联网用户对于产品的体验要求越来越高,作为一名美图秀秀级别选手,很难领悟“好设计 ...

  4. runloop源代码

    https://github.com/zzf073/runloopDemo /** *  调度例程 *  当将输入源安装到run loop后,调用这个协调调度例程,将源注册到客户端(可以理解为其他线程 ...

  5. 将数据内容动态添加到HTML中

    // 申明一个数组用来装遍历的元素 var li = []; //遍历元素并加载到标签中 for(var i = 0; i<navGroup.self_first_nav.length; i++ ...

  6. node——模块分类,require执行顺序,require注意事项,原理

    node.js模块 在node.js开发中一个文件就可以认为是一个模块. 一.node.js模块分类 核心模块Code Module.内置模块.原生模块 fs http path url ... 所有 ...

  7. node——npm

    npm 1.npm是nodejs的包管理器 2.npm 有很多代码仓库和代码模块 3.npm有一个npm客户端 4.npm本身也是基于Node.js开发的包 5.npm install npm@lat ...

  8. 10、Latent Relational Metric Learning via Memory-based Attention for Collaborative Ranking-----基于记忆注意的潜在关系度量协同排序

    一.摘要: 本文模型 LRML(潜在相关度量学习)是一种新的度量学习方法的推荐.[旨在学习用户和项目之间的相关关系,而不是简单的用户和项目之间的push和pull关系,push和pull主要针对LMN ...

  9. BZOJ 4182 Shopping (点分治+树上多重背包)

    题目大意:给你一颗树,你有$m$元钱,每个节点都有一种物品,价值为$w$,代价为$c$,有$d$个,如果在$u$和$v$两个城市都购买了至少一个物品,那么$u,v$路径上每个节点也都必须买至少一个物品 ...

  10. SparkSql初级编程实践

    1.Spark SQL 基本操作将下列 JSON 格式数据复制到 Linux 系统中,并保存命名为 employee.json.{ "id":1 , "name" ...