@kakadee
2017-04-19T02:54:04.000000Z
字数 6712
阅读 1396
iOS
Swift
前言:
swift3.0 学习笔记主要参考苹果开发者官网The Swift Programming Language (Swift 3.1)教程 以及 Swift 中文网。
更纯粹的阅读的体验请移步至:
https://www.zybuluo.com/kakadee/note/727524
函数是执行特定任务的代码自包含块。给定一个函数名称标识, 当执行其任务时就可以用这个标识来进行"调用"。
当你定义一个函数时,你可以为其定义一个或多个命名,定义类型值作为函数的输入(称为参数),当该函数完成时将传回输出定义的类型(称为作为它的返回类型)。
下面是一个greet(person:)
函数,输入参数是一个名字叫person
的String
类型。->
之后跟的是返回值类型,这里的返回值类型是一个String
类型。
func greet(person: String) -> String {
let greeting = "Hello, " + person + "!"
return greeting
}
print(greet(person: "Anna"))
// Prints "Hello, Anna!"
print(greet(person: "Brian"))
// Prints "Hello, Brian!"
在swift中函数的参数和返回值是非常具有灵活性的。你可以定义任何东西无论是一个简单的仅仅有一个未命名的参数的函数还是那种具有丰富的参数名称和不同的参数选项的复杂函数。
func sayHelloWorld() -> String {
return "hello, world"
}
print(sayHelloWorld())
// Prints "hello, world"
func greet(person: String, alreadyGreeted: Bool) -> String {
if alreadyGreeted {
return greetAgain(person: person)
} else {
return greet(person: person)
}
}
print(greet(person: "Tim", alreadyGreeted: true))
// Prints "Hello again, Tim!"
func greet(person: String) {
print("Hello, \(person)!")
}
greet(person: "Dave")
// Prints "Hello, Dave!"
提示:严格地说,这里的greet函数确实还返回一个值,即使没有返回值定义。函数没有定义返回类型但返 回了一个void返回类型的特殊值。它是一个空的元组,可以写为()。
你可以使用一个元组类型作为函数的返回类型返回一个有多个值组成的一个复合作为返回值。
func minMax(array: [Int]) -> (min: Int, max: Int) {
var currentMin = array[0]
var currentMax = array[0]
for value in array[1..<array.count] {
if value < currentMin {
currentMin = value
} else if value > currentMax {
currentMax = value
}
}
return (currentMin, currentMax)
}
如果返回的值可能为空的,那么你可以使用可选元组返回类型,即在元组类型后面加一个?
号。比如(Int, Int)?
or (String, Int, Bool)?
上述的minMax(array:) 函数并没有对传入的数组做安全检查,当传入一个空数组时就明显悲剧了(触发runtime error)。为了应对这种情况,使用一个可选元组作为返回类型,当传入数组为空时返回nil即可解决问题。
func minMax(array: [Int]) -> (min: Int, max: Int)? {
if array.isEmpty { return nil }
var currentMin = array[0]
var currentMax = array[0]
for value in array[1..<array.count] {
if value < currentMin {
currentMin = value
} else if value > currentMax {
currentMax = value
}
}
return (currentMin, currentMax)
}
func someFunction(firstParameterName: Int, secondParameterName: Int) {
// In the function body, firstParameterName and secondParameterName
// refer to the argument values for the first and second parameters.
}
someFunction(firstParameterName: 1, secondParameterName: 2)
和之前的版本不一样,每个函数参数都得有参数名,以便于更好的标识。
func someFunction(argumentLabel parameterName: Int) {
// In the function body, parameterName refers to the argument value for that parameter.
}
func greet(person: String, from hometown: String) -> String {
return "Hello \(person)! Glad you could visit from \(hometown)."
}
print(greet(person: "Bill", from: "Cupertino"))
// Prints "Hello Bill! Glad you could visit from Cupertino."
_
。
func someFunction(_ firstParameterName: Int, secondParameterName: Int) {
// In the function body, firstParameterName and secondParameterName
// refer to the argument values for the first and second parameters.
}
someFunction(1, secondParameterName: 2)
func someFunction(parameterWithoutDefault: Int, parameterWithDefault: Int = 12) {
// If you omit the second argument when calling this function, then
// the value of parameterWithDefault is 12 inside the function body.
}
someFunction(parameterWithoutDefault: 3, parameterWithDefault: 6) // parameterWithDefault is 6
someFunction(parameterWithoutDefault: 4) // parameterWithDefault is 12
提示:函数可以最多有一个可变参数的参数,而且它必须出现在参数列表的最后以避免多参数函 数调用时出现歧义。如果函数有一个或多个参数使用默认值,并且还具有可变参数,将可变参数放在列表的 最末尾的所有默认值的参数之后。
...
表示。
func arithmeticMean(_ numbers: Double...) -> Double {
var total: Double = 0
for number in numbers {
total += number
}
return total / Double(numbers.count)
}
arithmeticMean(1, 2, 3, 4, 5)
// returns 3.0, which is the arithmetic mean of these five numbers
arithmeticMean(3, 8.25, 18.75)
// returns 10.0, which is the arithmetic mean of these three numbers
inout
参数。
var someInt = 3
var anotherInt = 107
swapTwoInts(&someInt, &anotherInt)
print("someInt is now \(someInt), and anotherInt is now \(anotherInt)")
// Prints "someInt is now 107, and anotherInt is now 3"
注意:inout参数不能有默认值,并且可变参数不可以声明为inout.
每一个函数都有特定的函数类型,可以充当参数类型和函数的返回类型。例如:
func addTwoInts(a: Int, b: Int) -> Int {
return a + b
}
func multiplyTwoInts(a: Int, b: Int) -> Int {
return a * b
}
这个例子中定义了两个简单的数学函数addTwoInts和multiplyTwoInts。每个函数接受两个int值,并返回一个int值,执行适当的数学运算并返回结果。
这两个函数的类型是(Int, Int)->Int。可以解读为:"这个函数类型,它有两个int型的参数,并返回一个int类型的值。"
下面是另一个例子,不带任何参数或返回值的函数:
func printHelloWorld() {
println("hello, world")
}
这个函数的类型是()->(),或者"函数没有参数,并返回void。"函数不显式的指出一个返回值类型是void,在swift中相当于一个空元组,显示为()。
在swift中您可以像任何其他类型一样的使用函数类型。例如,你可以定义一个常量或变量为一个函数类型,并指定适当的函数给该变量:
var mathFunction: (Int, Int) -> Int = addTwoInts
该addTwoInts函数具有与mathFunction相同类型的变量,所以这个赋值在能通过swift的类型检查。
现在你可以调用指定的函数名称为mathFunction:
print("Result: \(mathFunction(2, 3))")
// prints "Result: 5"
您可以使用一个函数类型,如(Int, Int)->Int作为另一个函数的参数类型。这使你预留了一个函数的某些方面的函数实现,让调用者提供的函数时被调用。
下面就以打印上面的数学函数的结果为例:
func printMathResult(mathFunction: (Int, Int) -> Int, a: Int, b: Int) {
print("Result: \(mathFunction(a, b))")
}
printMathResult(addTwoInts, 3, 5)
// prints "Result: 8"
这个例子中定义了一个名为printMathResult函数,它有三个参数。第一个参数名为mathFunction,类型为(Int, Int)->Int。您可以传入符合的任何函数类型作为此函数的第一个参数。第二和第三个参数a、b都是int类型。被用作用于提供数学函数的两个输入值。
您可以使用一个函数类型作为另一个函数的返回类型。返回的函数-(>)即你的返回箭头后,立即写一个完整的函数类型就做到这一点。
下面的例子定义了两个简单的函数调用stepForward和stepBackward。该stepForward函数返回一个值高于其输入值,而stepBackward函数返回一个值低于其输入值。这两个函数都有一个相同的类型 (Int) -> Int:
func stepForward(input: Int) -> Int {
return input + 1
}
func stepBackward(input: Int) -> Int {
return input - 1
}
这里有一个chooseStepFunction函数,它的返回类型是"函数类型(Int) -> Int"。chooseStepFunction返回一个基于布尔参数的stepBackward或stepForward函数类型:
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
前面的例子可以判断正负的步骤决定是否需要移动一个名为使得currentValue变量逐步接近零。currentValue初始值是3,这意味着当前值>0,则返回true,chooseStepFunction返回stepBackward函数。返回函数的引用存储在一个称为moveNearerToZero常量里。
如今moveNearerToZero执行了正确的功能,就可以用来计数到零:
print("Counting to zero:")
// Counting to zero:
while currentValue != 0 {
print("\(currentValue)... ")
currentValue = moveNearerToZero(currentValue)
}
println("zero!")
// 3...
// 2...
// 1...
// zero!
迄今为止所有你在本章中遇到函数都是全局函数,在全局范围内定义。其实你还可以在其他函数中定义函数,被称为嵌套函数。
嵌套函数默认对外界是隐藏的,但仍然可以调用和使用其内部的函数。内部函数也可以返回一个嵌套函数,允许在嵌套函数内的另一个范围内使用。
你可以重写上面的chooseStepFunction例子使用并返回嵌套函数:
func chooseStepFunction(backward: Bool) -> (Int) -> Int {
func stepForward(input: Int) -> Int { return input + 1 }
func stepBackward(input: Int) -> Int { return input - 1 }
return backward ? stepBackward : stepForward
}
var currentValue = -4
let moveNearerToZero = chooseStepFunction(backward: currentValue > 0)
// moveNearerToZero now refers to the nested stepForward() function
while currentValue != 0 {
print("\(currentValue)... ")
currentValue = moveNearerToZero(currentValue)
}
print("zero!")
// -4...
// -3...
// -2...
// -1...
// zero!