swift语言点评五-Function
一、函数类型
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:
func addTwoInts(_ a: Int, _ b: Int) -> Int {
return a + b
}
func multiplyTwoInts(_ a: Int, _ b: Int) -> Int {
return a * b
}
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:
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:
func printMathResult(_ mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {
print("Result: \(mathFunction(a, b))")
}
printMathResult(addTwoInts, 3, 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
func greet(person: String, from hometown: String) -> String {
return "Hello \(person)! Glad you could visit from \(hometown)."
}
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的更多相关文章
- Swift语言指南(五)--数字字面量和数字类型转换
原文:Swift语言指南(五)--数字字面量和数字类型转换 数字字面量 整数字面量写法如下: · 十进制数,无前缀 · 二进制数,以 0b 为前缀 · 八进制数,以 0o 为前缀 · 十六进制数,以 ...
- swift语言点评十五-$0
import UIKitimport XCPlayground class ViewController: UIViewController { func action() { print(" ...
- swift语言点评四-Closure
总结:整个Closure的作用在于简化语言表述形式. 一.闭包的简化 Closure expression syntax has the following general form: { () -& ...
- swift语言点评一
一.变量定义 1.常量与变量 Use let to make a constant and var to make a variable. 2.类型与推测 However, you don’t alw ...
- swift语言点评十九-类型转化与检查
1.oc比较: -(BOOL) isKindOfClass: classObj判断是否是这个类或者这个类的子类的实例 -(BOOL) isMemberOfClass: classObj 判断是否是这个 ...
- swift语言点评十八-异常与错误
1.错误类型与枚举的结合 enum VendingMachineError: Error { case invalidSelection case insufficientFunds(coinsNee ...
- swift语言点评二
一.数据类型 1.基础类型的封装 Swift provides its own versions of all fundamental C and Objective-C types, includi ...
- swift语言点评十七-Designated Initializers and Convenience Initializers
Swift defines two kinds of initializers for class types to help ensure all stored properties receive ...
- swift语言点评十六-Initialization && Deinitialization
initial value:必须初始化.不影响观察者 Classes and structures must set all of their stored properties to an appr ...
随机推荐
- Windows下Java JDK安装和环境变量配置
[Java攻城狮学习路线](http://www.cnblogs.com/apollospotatolikett/p/8665123.html 1.JDK下载 下载地址:http://www.orac ...
- C++内存分配方式——小结
1 内存分配方式 内存分配方式有如下三种: 从静态存储区域分配.内存在程序编译的时候就分配好了,这些内存在整个程序运行期间都存在,如全局变量.static变量等等. 在堆栈上分配.在函数执行期间,函数 ...
- ABBYY简体中文版终身授权半价来袭,真的是5折!
经过了一个春秋,心心念念的双十一终于要来了,一年时间并不长,但这一个月尤其慢!ABBYY官方称为回馈广大用户的支持与厚爱,双十一期间,ABBYY价格感人,诱惑难挡. 说到双十一活动,方式也是五花八门, ...
- ZBrush软件特性之Stencil模板调控板
在ZBrush中使用Stencil模板我们了解的所有绘图工具,通过它确定模板周围与涂画或模型的位置.本文将详解ZBrush®中如何使用“曲线板”自定义形状. 使用模版 ZBrush模版的作用象我们了解 ...
- CSS——背景图像区域
background-clip属性 background-clip属性指定背景绘制区域 语法 background-clip:border-box|padding-box|content-box; b ...
- NTP同步底层实现
RFC http://www.ietf.org/rfc/rfc5905.txt https://www.eecis.udel.edu/~mills/ntp/html/select.html https ...
- JS 将有父子关系的数组转换成树形结构数据
将类似如下数据转换成树形的数据 [{ id: 1, name: '1', }, { id: 2, name: '1-1', parentId: 1 }, { id: 3, name: '1-1-1', ...
- 【Python 学习】通过while循环和for循环猜测年龄
Python中可以通过while和for循环来限制猜测年龄的次数 1. 在猜测年龄的小程序中,不加循环的代码是这样的: age_of_yu = 23 guess_age = int(input(&qu ...
- 集成swagger2构建Restful API
集成swagger2构建Restful API 在pom.xml中进行版本管理 <swagger.version>2.8.0</swagger.version> 给taosir ...
- webpack的热更新
webpack的热更新是如何做到的?说明其原理? webpack的热更新又称热替换(Hot Module Replacement),缩写为HMR. 这个机制可以做到不用刷新浏览器而将新变更的模块替换掉 ...