如何创建引用特定函数的函数类型的类型

SLN*_*SLN 5 types function swift

斯威夫特的官方文件

您可以像使用Swift中的任何其他类型一样使用函数类型.例如,您可以将常量或变量定义为函数类型,并为该变量分配适当的函数:

func addTwoInts(a: Int, _ b: Int) -> Int {
    return a + b
}

var mathFunction: (Int, Int) -> Int = addTwoInts
Run Code Online (Sandbox Code Playgroud)

这里是示例代码:

它定义一个名为的变量mathFunction,它具有一个带两个Int值的函数类型,并返回一个Int值.设置此新变量以引用名为addTwoInts的函数

问题:函数类型可以像Swift中的任何其他类型一样使用,我想知道,因为我如何创建一个类型别名,它具有一个带有两个Int值的函数类型,并返回一个Int值.设置此新变量以引用名为addTwoInts的函数

我试过这个,显然,我错了.

在此输入图像描述

Cod*_*ode 10

您无法为a分配功能typealias.

您只能指定一种类型.

typealias mathFunctionType = (Int, Int) -> Int

let mathFunction: mathFunctionType = { int1, int2 in
    return int1 + int2
}
Run Code Online (Sandbox Code Playgroud)