问题描述
我正在阅读 Apple 对 Swift 的介绍,并遇到了这样的例子:
I was going through Apple's introduction to Swift and came across such example:
func makeIncrementer() -> ((Int) -> Int) {
func addOne(number: Int) -> Int {
return 1 + number
}
return addOne
}
var increment = makeIncrementer()
increment(7)
你能解释一下 makeIncrementer 函数的返回类型的语法吗?我知道这个函数返回另一个函数,但是 ((Int) -> Int)
在这个上下文中的作用对我来说仍然不清楚.
Can you please explain the syntax of the return type of the makeIncrementer function? I understand that this function returns another function, but there role of ((Int) -> Int)
in this context is still unclear for me.
推荐答案
表示函数返回一个函数,返回的函数以Int
作为输入参数并返回一个 Int
.
It indicates that the function returns a function and that returned function takes an Int
as an input parameter and returns an Int
as well.
在函数内定义函数在 Swift 中是完全合法的.
Defining functions within functions is perfectly legal in Swift.
这篇关于类型 ((Int) -> Int) 在 Swift 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!