PS: 最近开始系统的阅读《The Swift Programming Language》(Swift 2 Prerelease),阅读过程中对知识点做一定的梳理,便于查阅。对自己的要求是:持续更新。(码字挺累,效果很好)
方法定义的格式
func sayHello(personName: String) -> String {
let greeting = "Hello, " + personName + "!"
return greeting
}
其中,func为关键字,方法名为sayHello(_:),形参为String类型的personName(多个参数时以 ","隔开),返回值为String类型(无返回值则去掉"->String")。
返回多值(元组)
swift中方法利用元组返回多个参数,如下:
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)
}
但是,上面的方法没有进行异常处理,理所当然的在方法的第一行加一句:
if array.isEmpty {
return nil;
}
方法标签
方法参数都有一个外部参数名和一个本地参数名(两者以逗号隔开),外部参数名即Objective-C语法中的标签,本地参数名即在方法中使用的参数名。默认情况下,如果没有指定外部参数名,swift会把除了第一个参数外的其它本地参数名变为外部参数名。上代码:
func someFunction(firstParamName: Int, secondParamName: Int) {
// do something.
}
someFunction(1, secondParamName: 2)
其中firstParamName和secondParamName都为本地参数名,但是在调用时可以发现,第一个参数没有外部参数名,第二个参数将
本地参数名变为外部参数名,此时方法名为someFunction(_:secondParamName:)。实际开发中建议为每一个参数指定外部参数名(标签),增加程序的可读性:
func sayHello(to person: String, and anotherPerson: String) -> String {
return "Hello \(person) and \(anotherPerson)"
}
sayHello(to: "Bill", and: "Ted")
方法名为sayHello(to:and:),在调用方法时,能够见名思义。
那么问题来了,如果不想有外部参数名(标签)怎么办?很简单,在标签位置使用下划线"_"作为标签名即可。例如:
func someFunction(firstParamName: Int, _ secondParamName: Int) {
// do something.
}
someFunction(1, 2)
方法名变为someFunction(_:_:)。
参数默认值
跟C++一样,swift方法的参数也可以指定默认值,但是该参数只能是最后的1个或几个参数,上代码:
func someFunction(first: Int, seceond: Int = 20, third: Int = 30) {
// do something.
}
someFunction(10)
下面的写法也没有错,然并卵,因为第一个参数必须赋值:
fun someFunction(first: Int = 10, second: Int, third: Int) {
// do something.
}
可变参数
swift的方法形参个数是可变的,先上代码:
func arithmeticMean(numbers: Double...) -> Double {
var total: Double = 0
for number in numbers {
total += number
}
return total / Double(numbers.count)
}
print(arithmeticMean(1, 2, 3, 4, 5)) // 3.0
print(arithmeticMean(3, 8.25, 18.75)) // 10.0
其实,编译器会将传入的多个参数作为一个数组传入,这样就好理解了。
常量和变量参数
在其他语言中,方法的形参默认是可变的,即在方法中可以修改值,但swift默认指定形参为常量,方法中不能修改,如果需要指定一个形参为变量,使用var关键字修饰即可:
func someFunction(var str: String) -> String {
str += str + " World."
return str
}
这是什么鬼?其实就是c语言的引用型参数,swift中需要在参数前面使用inout关键字修饰,直接上代码理解一下:
func swapTwoInts(inout a: Int, inout _ b: Int) {
let tempA = a
a = b
b = tempA
}
var a = 123
var b = 456
swapTwoInts(&a, &b)
print("a = \(a) b = \(b)")
懂了吧!
方法类型(Function Types)
这又是什么鬼?叫的很玄乎,就是将方法视为同Double,Int,String等一样的数据类型(不就是函数指针么)。
先声明一个方法:
func addTwoInts(a: Int, _ b: Int) -> Int {
return a + b
}
1、作为普通变量
var mathFunction: (Int, Int) -> Int = addTwoInts; // 或者var mathFunction = addTwoInts,编译器会自动判断类型为(Int, Int) -> Int。
print(mathFunction(3, 4))
2、作为形参
func printMathResult(mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {
print("Result: \(mathFunction(a, b))")
}
printMathResult(addTwoInts, 3, 5) // "Result: 8"
3、作为返回值
func stepForward(input: Int) -> Int {
return input + 1
}
func stepBackward(input: Int) -> Int {
return input - 1
}
func chooseStepFunction(backforward: Bool) -> (Int) -> Int {
return backforward ? stepBackward : stepForward
}
let stepFunction = chooseStepFunction(true)
print(stepFunction(1)) // 结果为0
4、方法嵌套
func getMathFunction() -> (Int, Int) -> Int {
func addTwoInts(a: Int, _ b: Int) -> Int {return a + b}
return addTwoInts
}
let mathFunction = getMathFunction()
print(mathFunction(5, 6)) //结果为11
值得一提的是,如果参数有标签,赋值给方法类型变量的时候,标签被忽略,但被赋值的变量调用时,必须带上标签。说一堆不如几行代码来得快:
func addTwoInts(a a: Int, b: Int) -> Int {
return a + b
}
let mathFunction = addTwoInts // 赋值时不带标签
mathFunction(a: 2, b: 3) // 调用时带标签