Welcome to Swift (苹果官方Swift文档初译与注解三十五)---248~253页(第五章-- 函数 完)
2014-07-19 20:44
417 查看
Function Types as Return Types (函数类型作为返回值类型)
一个函数的类型可以作为另一个函数的返回值类型.可以在一个函数的返回值箭头后面写上一个完整的函数类型.
例如:
下面的例子定义了两个简单的函数,分别为stepForward 和 stepBackward.其中stepForward函数返回值比它的输入值多1,stepBackward函数返回值比它输入值少1.这两个函数的 类型都是(Int) -> Int:
func stepForward(input: Int) -> Int {
return input + 1
}
func stepBackward(input: Int) -> Int {
return input - 1
}
这里有一个函数chooseStepFunction,它的返回类型是一个函数类型(Int) -> Int.函数chooseStepFunction根据它的布尔类型的参数返回stepForward函数类型或者stepBackward函 数类型 :
func chooseStepFunction(backwards: Bool) -> (Int) -> Int {
return backwards ? stepBackward : stepForward
}
现在你可以使用chooseStepFunction获得一个函数:
var currentValue = 3
let moveNearerToZero = chooseStepFunction(currentValue > 0)
// moveNearerToZero now refers to the stepBackward() function
moveNearerToZero将根据适当的函数来计算,一直到零:
println("Counting to zero:")
// Counting to zero:
while currentValue != 0 {
println("\(currentValue)... ")
currentValue = moveNearerToZero(currentValue)
}
println("zero!")
// 3...
// 2...
// 1...
// zero!
Nested Functions (函数嵌套)
你可以在一个函数体内定义另一个函数,这种情况叫做嵌套函数.
默认情况下,嵌套的函数对外界是不知的.但可以通过封装它的函数来调用它.封装它的函数可以将它作为函数的返回值返回,这样可以允许嵌套的函数在其他范围使用.
代码样例:
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
}
var currentValue = -4
let moveNearerToZero = chooseStepFunction(currentValue > 0)
// moveNearerToZero now refers to the nested stepForward() function
while currentValue != 0 {
println("\(currentValue)... ")
currentValue = moveNearerToZero(currentValue)
}
println("zero!")
// -4...
// -3...
// -2...
// -1...
// zero!
/********************本章完......************************/
一个函数的类型可以作为另一个函数的返回值类型.可以在一个函数的返回值箭头后面写上一个完整的函数类型.
例如:
下面的例子定义了两个简单的函数,分别为stepForward 和 stepBackward.其中stepForward函数返回值比它的输入值多1,stepBackward函数返回值比它输入值少1.这两个函数的 类型都是(Int) -> Int:
func stepForward(input: Int) -> Int {
return input + 1
}
func stepBackward(input: Int) -> Int {
return input - 1
}
这里有一个函数chooseStepFunction,它的返回类型是一个函数类型(Int) -> Int.函数chooseStepFunction根据它的布尔类型的参数返回stepForward函数类型或者stepBackward函 数类型 :
func chooseStepFunction(backwards: Bool) -> (Int) -> Int {
return backwards ? stepBackward : stepForward
}
现在你可以使用chooseStepFunction获得一个函数:
var currentValue = 3
let moveNearerToZero = chooseStepFunction(currentValue > 0)
// moveNearerToZero now refers to the stepBackward() function
moveNearerToZero将根据适当的函数来计算,一直到零:
println("Counting to zero:")
// Counting to zero:
while currentValue != 0 {
println("\(currentValue)... ")
currentValue = moveNearerToZero(currentValue)
}
println("zero!")
// 3...
// 2...
// 1...
// zero!
Nested Functions (函数嵌套)
你可以在一个函数体内定义另一个函数,这种情况叫做嵌套函数.
默认情况下,嵌套的函数对外界是不知的.但可以通过封装它的函数来调用它.封装它的函数可以将它作为函数的返回值返回,这样可以允许嵌套的函数在其他范围使用.
代码样例:
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
}
var currentValue = -4
let moveNearerToZero = chooseStepFunction(currentValue > 0)
// moveNearerToZero now refers to the nested stepForward() function
while currentValue != 0 {
println("\(currentValue)... ")
currentValue = moveNearerToZero(currentValue)
}
println("zero!")
// -4...
// -3...
// -2...
// -1...
// zero!
/********************本章完......************************/
相关文章推荐
- Welcome to Swift (苹果官方Swift文档初译与注解三十三)---235~240页(第五章-- 函数)
- Welcome to Swift (苹果官方Swift文档初译与注解二十九)---209~218页(第四章-- 流程控制)
- Welcome to Swift (苹果官方Swift文档初译与注解三十一)---225~230页(第五章-- 函数)
- Welcome to Swift (苹果官方Swift文档初译与注解十二)---77~83页(第二章)
- Welcome to Swift (苹果官方Swift文档初译与注解七)---45~52页(第二章)
- Welcome to Swift (苹果官方Swift文档初译与注解三十四)---241~247页(第五章-- 函数)
- Welcome to Swift (苹果官方Swift文档初译与注解三十二)---230~234页(第五章-- 函数)
- Welcome to Swift (苹果官方Swift文档初译与注解六)---35~44页 (第一章完)
- Welcome to Swift (苹果官方Swift文档初译与注解十三)---84~89页(第二章)
- Welcome to Swift (苹果官方Swift文档初译与注解二十五)---171~180页(第四章 流程控制)
- Welcome to Swift (苹果官方Swift文档初译与注解十五)---94~100页(第二章)
- Welcome to Swift (苹果官方Swift文档初译与注解十九)---123~132页(第二章..本章节还剩6页)
- Welcome to Swift (苹果官方Swift文档初译与注解三)---11~18页
- Welcome to Swift (苹果官方Swift文档初译与注解四)---19~28页
- Welcome to Swift (苹果官方Swift文档初译与注解二十二)---148~153页(第三章--集合类型)
- Welcome to Swift (苹果官方Swift文档初译与注解二十六)---181~188页(第四章-- 流程控制)
- Welcome to Swift (苹果官方Swift文档初译与注解十八)---116~122页(第二章)
- Welcome to Swift (苹果官方Swift文档初译与注解五)---29~34页
- Welcome to Swift (苹果官方Swift文档初译与注解二十)---133~139页(第二章..本节完)
- Welcome to Swift (苹果官方Swift文档初译与注解九)---58~62页(第二章)