本篇是《Swift語法學(xué)習(xí)筆記》系列的第三篇文章,將涵蓋以下內(nèi)容:
- 函數(shù)和閉包
- 函數(shù)
- 一般形式
- 無參怀读、無返回值的函數(shù)
- 函數(shù)調(diào)用
- 含有默認(rèn)參數(shù)值的函數(shù)
- 內(nèi)部參數(shù)與外部參數(shù)
- 內(nèi)部參數(shù)與外部參數(shù)小節(jié)
- 一個參數(shù)
- 兩個及以上參數(shù)
- 閉包
- 函數(shù)變量
- 函數(shù)變量作為參數(shù)
- 閉包
- 閉包的作用域
1 函數(shù)
1.1 一般形式
func areaOfRectangle(width: Double, height: Double) -> Double {
return width * height
}
1.2 無參渤早、無返回值的函數(shù)
func hello() {
print("Hello, world")
}
或者
func hello() -> Void {
print("Hello, world")
}
1.3 函數(shù)調(diào)用
一般無需提供第一個參數(shù)的名稱(1.5小節(jié)會詳細(xì)解釋)
let area = areaOfRectangle(20, height: 10)
1.4 含有默認(rèn)參數(shù)值的函數(shù)
功能描述:使用分隔符delimiter將傳入的字符串分割成字符串?dāng)?shù)組,默認(rèn)分隔符為空格:
func separateWords(str: String, delimiter: String = " ") -> [String] {
return str.componentsSeparatedByString(delimiter)
}
使用方法:
let result = separateWords("One small step")
print(result) // [One, small, step]
或者
let result = separateWords("One. Two. Three", delimiter: ". ") // "delimiter" is required
print(result) // [One, Two, Three]
1.5 內(nèi)部參數(shù)與外部參數(shù)
下面的例子中檩互,width與height是外部參數(shù)如迟,用于函數(shù)調(diào)用;w和h是內(nèi)部參數(shù)奢人,用于函數(shù)內(nèi)部使用:
func areaOfRectangle(width w: Double, height h: Double) -> Double {
return w * h
}
調(diào)用方法:
let area = areaOfRectangle(width: 20, height: 10)
Swift automatically supplies an external name for an argument with a default value, making it the same as the internal name (although you can override this by supplying your own external name). That means that the argument name must be used if its value is not defaulted.
可以將外部參數(shù)設(shè)置為空:
func separateWords(str: String, _ delimiter: String = " ") -> [String] {
return str.componentsSeparatedByString(delimiter)
}
let result = separateWords("One. Two. Three", ". ")
print(result)
1.6 內(nèi)部參數(shù)與外部參數(shù)小結(jié)
- 一個參數(shù)
-
兩個以上參數(shù)
未指定外部參數(shù)(參考1.1小節(jié))谓媒。調(diào)用時無需指定第一個參數(shù)的名字(編譯器已經(jīng)為第一個參數(shù)指定了默認(rèn)空的外部參數(shù)名稱),但是必須指定第二個參數(shù)的外部參數(shù)名字(編譯器會自動創(chuàng)建第二個參數(shù)的外部名字和內(nèi)部名字一樣)达传;
指定了外部參數(shù)名字篙耗。調(diào)用時就必須指定外部參數(shù)名字;
指定外部名字為空“-”宪赶,調(diào)用時無需指定參數(shù)名字宗弯。func areaOfRectangle(w: Double, _ h: Double) -> Double {
return w * h
}
let area = areaOfRectangle(20, 10)
編譯器默認(rèn)第一個參數(shù)的外部參數(shù)為空,除非用戶指定了非空外部參數(shù)名字:
func areaOfRectangle(_ w: Double, _ h: Double) -> Double { // OK, but gets a warning
return w * h
}
2 閉包
2.1 函數(shù)變量
In Swift, functions are types, so you can create a variable of function type, assign a reference to a function to it, and use that variable to call the function. Similarly, you can pass a function as an argument to another function or return a function from a function.
聲明一個輸入?yún)?shù)為Double類型搂妻,返回值為Double類型的函數(shù)變量:
var operation: (Double) -> Double
或者
var operation: Double -> Double
使用該函數(shù)變量:
func doubleMe(number: Double) -> Double {
return 2 * number
}
operation = doubleMe
operation(2) // Result is 4
指向另外一個函數(shù):
func quadrupleMe(number: Double) -> Double {
return 4 * number
}
operation = quadrupleMe
operation(2) // Result is 8
2.2 函數(shù)變量作為參數(shù)
首先蒙保,需要定義一個函數(shù):
func compareInts(first: Int, second: Int) -> Bool {
return first < second
}
創(chuàng)建函數(shù)變量,并傳遞到函數(shù)內(nèi)部:
var values = [12, 3, 5, -4, 16, 18]
let sortedValues = values.sort(compareInts)
sortedValues // Result: [-4, 3, 5, 12, 16, 18]
或者
var values = [12, 3, 5, -4, 16, 18]
values.sortInPlace(compareInts)
values // Result: [-4, 3, 5, 12, 16, 18]
2.3 閉包
將2.2小節(jié)中的函數(shù)聲明欲主、函數(shù)變量定義與函數(shù)變量的傳遞合并在一起:
var values = [12, 3, 5, -4, 16, 18]
let sorted = values.sort({(first: Int, second: Int) -> Bool in
return first < second
})
或者
let sorted = values.sort() { // The closure is now outside the parentheses
(first: Int, second: Int) -> Bool in
return first < second
}
或者
let sorted = values.sort() {
first, second in // Swift infers the argument types and return type!
return first < second
}
或者
let sorted = values.sort() { return $0 < $1 }
甚至
let sorted = values.sort() { $0 < $1 }
2.4 閉包的作用域
func getInterestCalculator(rate: Double) -> (Double, Int) -> Double {
let calculator = {(amount: Double, years: Int) -> Double in rate * amount * Double(years)}
return calculator
}
調(diào)用
let calculator = getInterestCalculator(0.05)
calculator(100.0, 2) // Result is 10: interest at 5% on $100 over 2 years.
參考文獻(xiàn)
《Beginning iPhone Development with Swift 2 - Exploring the iOS SDK 》From page 777 to page 838.
聯(lián)系作者
- 評論
- xueyublue@gmail.com
- qq905375641