// 跟java 里的范型一樣
func swapTwoInts(_ a: inout Int, _ b: inout Int) {
let temporaryA = a
a = b
b = temporaryA
}
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"
//swapTwoInts(::) 函數(shù)很實(shí)用电湘,但是它只能用于 Int 值踏幻。如果你想交換兩個(gè) String 值,或者兩個(gè) Double 值生音,你只能再寫(xiě)更多的函數(shù)韩脏,
//為了解決這個(gè)問(wèn)題缩麸,引入范型
泛型函數(shù)語(yǔ)法
func swapTwoValues<T>(_ a: inout T, _ b: inout T) {
let temporaryA = a
a = b
b = temporaryA
}
// 對(duì)比
func swapTwoInts(_ a: inout Int, _ b: inout Int)
func swapTwoValues<T>(_ a: inout T, _ b: inout T)
使用范型
var someInt = 3
var anotherInt = 107
swapTwoValues(&someInt, &anotherInt)
// someInt is now 107, and anotherInt is now 3
var someString = "hello"
var anotherString = "world"
swapTwoValues(&someString, &anotherString)
// someString is now "world", and anotherString is now "hello"
eg2:
struct IntStack {
var items = [Int]()
mutating func push(_ item: Int) {
items.append(item)
}
mutating func pop() -> Int {
return items.removeLast()
}
}
struct Stack<Element> {
var items = [Element]()
mutating func push(_ item: Element) {
items.append(item)
}
mutating func pop() -> Element {
return items.removeLast()
}
}
// using
var stackOfStrings = Stack<String>()
stackOfStrings.push("uno")
stackOfStrings.push("dos")
stackOfStrings.push("tres")
stackOfStrings.push("cuatro")
// the stack now contains 4 strings
let fromTheTop = stackOfStrings.pop()
// fromTheTop is equal to "cuatro", and the stack now contains 3 strings
擴(kuò)展一個(gè)泛型類(lèi)型
struct Stack<Element> {
var items = [Element]()
mutating func push(_ item: Element) {
items.append(item)
}
mutating func pop() -> Element {
return items.removeLast()
}
}
extension Stack {
var topItem: Element? {
return items.isEmpty ? nil : items[items.count - 1]
}
}
if let topItem = stackOfStrings.topItem {
print("The top item on the stack is \(topItem).")
}
// Prints "The top item on the stack is tres."
類(lèi)型約束
語(yǔ)法
// 第一個(gè)類(lèi)型形式參數(shù), T 赡矢,有一個(gè)類(lèi)型約束要求 T 是 SomeClass 的子類(lèi)匙睹。
// 第二個(gè)類(lèi)型形式參數(shù), U 济竹,有一個(gè)類(lèi)型約束要求 U 遵循 SomeProtocol 協(xié)議痕檬。
func someFunction<T: SomeClass, U: SomeProtocol>(someT: T, someU: U) {
// function body goes here
}
類(lèi)型約束的應(yīng)用
func findIndex(ofString valueToFind: String, in array: [String]) -> Int? {
for (index, value) in array.enumerated() {
if value == valueToFind {
return index
}
}
return nil
}
let strings = ["cat", "dog", "llama", "parakeet", "terrapin"]
if let foundIndex = findIndex(ofString: "llama", in: strings) {
print("The index of llama is \(foundIndex)")
}
// Prints "The index of llama is 2"
// using 范型
// 這里寫(xiě)出了一個(gè)叫做 findIndex(of:in:) 的函數(shù),可能是你期望的 findIndex(ofString:in:) 函數(shù)的一個(gè)泛型版本送浊。
// 注意梦谜,函數(shù)的返回值仍然是 Int? 袭景,因?yàn)楹瘮?shù)返回一個(gè)可選的索引數(shù)字唁桩,而不是數(shù)組里的一個(gè)可選的值。
func findIndex<T>(of valueToFind: T, in array:[T]) -> Int? {
for (index, value) in array.enumerated() {
if value == valueToFind {
return index
}
}
return nil
}
/*
這個(gè)函數(shù)沒(méi)有像上面寫(xiě)的那樣編譯耸棒。問(wèn)題在于相等檢查荒澡,” if value == valueToFind “。Swift 中的類(lèi)型不是每種都能用相等操作符( == )來(lái)比較的与殃。如果你創(chuàng)建自己的類(lèi)或者結(jié)構(gòu)體去描述一個(gè)復(fù)雜的數(shù)據(jù)模型单山,比如說(shuō),對(duì)于那個(gè)類(lèi)或結(jié)構(gòu)體來(lái)說(shuō)幅疼,”相等”的意義不是 Swift 能替你猜出來(lái)的米奸。因此,不能保證這份代碼可以用于所有 T 可以表示的類(lèi)型爽篷,當(dāng)你嘗試編譯這份代碼時(shí)會(huì)提示一個(gè)相應(yīng)的錯(cuò)誤悴晰。
*/
// 修正
// Swift 標(biāo)準(zhǔn)庫(kù)中定義了一個(gè)叫做 Equatable 的協(xié)議,要求遵循其協(xié)議的類(lèi)型要實(shí)現(xiàn)相等操作符( == )和不等操作符( != )逐工,用于比較該類(lèi)型的任意兩個(gè)值铡溪。所有Swift標(biāo)準(zhǔn)庫(kù)中的類(lèi)型自動(dòng)支持 Equatable 協(xié)議。
func findIndex<T: Equatable>(of valueToFind: T, in array:[T]) -> Int? {
for (index, value) in array.enumerated() {
if value == valueToFind {
return index
}
}
return nil
}
let doubleIndex = findIndex(of: 9.3, in: [3.14159, 0.1, 0.25])
// doubleIndex is an optional Int with no value, because 9.3 is not in the array
let stringIndex = findIndex(of: "Andrea", in: ["Mike", "Malcolm", "Andrea"])
// stringIndex is an optional Int containing a value of 2
關(guān)聯(lián)類(lèi)型 (associatedtype) -- 抽象類(lèi)型
protocol Container {
associatedtype ItemType
mutating func append(_ item: ItemType)
var count: Int { get }
subscript(i: Int) -> ItemType { get }
}
// IntStack 為了實(shí)現(xiàn) Container 協(xié)議泪喊,指定了適用于 ItemType 的類(lèi)型是 Int 類(lèi)型棕硫。 typealias ItemType = Int 把 ItemType 抽象類(lèi)型轉(zhuǎn)換為了具體的 Int 類(lèi)型。
struct IntStack: Container {
// original IntStack implementation
var items = [Int]()
mutating func push(_ item: Int) {
items.append(item)
}
mutating func pop() -> Int {
return items.removeLast()
}
// conformance to the Container protocol
typealias ItemType = Int
mutating func append(_ item: Int) {
self.push(item)
}
var count: Int {
return items.count
}
subscript(i: Int) -> Int {
return items[i]
}
}
// 你也可以做一個(gè)遵循 Container 協(xié)議的泛型 Stack 類(lèi)型:
struct Stack<Element>: Container {
// original Stack<Element> implementation
var items = [Element]()
mutating func push(_ item: Element) {
items.append(item)
}
mutating func pop() -> Element {
return items.removeLast()
}
// conformance to the Container protocol
mutating func append(_ item: Element) {
self.push(item)
}
var count: Int {
return items.count
}
subscript(i: Int) -> Element {
return items[i]
}
}
// 這次窘俺,類(lèi)型形式參數(shù) Element 用于 append(_:) 方法的 item 形式參數(shù)和下標(biāo)的返回類(lèi)型饲帅。因此,對(duì)于這個(gè)容器瘤泪,Swift可以推斷出 Element 是適用于 ItemType 的類(lèi)型灶泵。
擴(kuò)展現(xiàn)有類(lèi)型來(lái)指定關(guān)聯(lián)類(lèi)型
// 你可以擴(kuò)展一個(gè)現(xiàn)有類(lèi)型使其遵循一個(gè)協(xié)議,如在擴(kuò)展里添加協(xié)議遵循描述的一樣对途。這包括一個(gè)帶關(guān)聯(lián)類(lèi)型的協(xié)議赦邻。
/*
Swift 的 Array 類(lèi)型已經(jīng)提供了 append(_:) 方法、 count 屬性实檀、用 Int 索引取出其元素的下標(biāo)惶洲。這三個(gè)功能滿足了 Container 協(xié)議的要求。這意味著你可以通過(guò)簡(jiǎn)單地聲明 Array 采納協(xié)議膳犹,擴(kuò)展 Array 使其遵循 Container 協(xié)議恬吕。通過(guò)一個(gè)空的擴(kuò)展實(shí)現(xiàn),如使用擴(kuò)展聲明采納協(xié)議:
*/
extension Array: Container {}
/*
數(shù)組已有的 append(_:) 方法和下標(biāo)使得Swift能為 ItemType 推斷出合適的類(lèi)型须床,就像上面的泛型 Stack 類(lèi)型一樣铐料。定義這個(gè)擴(kuò)展之后,你可以把任何 Array 當(dāng)做一個(gè) Container 使用豺旬。
*/
泛型Where語(yǔ)句
func allItemsMatch<C1: Container, C2: Container>
(_ someContainer: C1, _ anotherContainer: C2) -> Bool
where C1.ItemType == C2.ItemType, C1.ItemType: Equatable {
// Check that both containers contain the same number of items.
if someContainer.count != anotherContainer.count {
return false
}
// Check each pair of items to see if they are equivalent.
for i in 0..<someContainer.count {
if someContainer[i] != anotherContainer[i] {
return false
}
}
// All items match, so return true.
return true
}
- C1 必須遵循 Container 協(xié)議(寫(xiě)作 C1: Container )钠惩;
- C2 也必須遵循 Container 協(xié)議(寫(xiě)作 C2: Container );
- C1 的 ItemType 必須和 C2 的 ItemType 相同(寫(xiě)作 C1.ItemType == C2.ItemType )族阅;
- C1 的 ItemType 必須遵循 Equatable 協(xié)議(寫(xiě)作 C1.ItemType: Equatable )篓跛。
// using
var stackOfStrings = Stack<String>()
stackOfStrings.push("uno")
stackOfStrings.push("dos")
stackOfStrings.push("tres")
var arrayOfStrings = ["uno", "dos", "tres"]
if allItemsMatch(stackOfStrings, arrayOfStrings) {
print("All items match.")
} else {
print("Not all items match.")
}
// Prints "All items match."
帶有泛型 Where 分句的擴(kuò)展
extension Stack where Element: Equatable {
func isTop(_ item: Element) -> Bool {
guard let topItem = items.last else {
return false
}
return topItem == item
}
}
if stackOfStrings.isTop("tres") {
print("Top element is tres.")
} else {
print("Top element is something else.")
}
// Prints "Top element is tres."
struct NotEquatable { }
var notEquatableStack = Stack<NotEquatable>()
let notEquatableValue = NotEquatable()
notEquatableStack.push(notEquatableValue)
notEquatableStack.isTop(notEquatableValue) // Error
// 你可以使用泛型 where 分句來(lái)擴(kuò)展到一個(gè)協(xié)議。下面的栗子把先前的 Container 協(xié)議擴(kuò)展添加了一個(gè) startsWith(_:) 方法坦刀。
extension Container where Item: Equatable {
func startsWith(_ item: Item) -> Bool {
return count >= 1 && self[0] == item
}
}
if [9, 9, 9].startsWith(42) {
print("Starts with 42.")
} else {
print("Starts with something else.")
}
// Prints "Starts with something else."
// 上邊栗子中的泛型 where 分句要求 Item 遵循協(xié)議愧沟,但你同樣可以寫(xiě)一個(gè)泛型 where 分句來(lái)要求 Item 為特定類(lèi)型。比如:
extension Container where Item == Double {
func average() -> Double {
var sum = 0.0
for index in 0..<count {
sum += self[index]
}
return sum / Double(count)
}
}
print([1260.0, 1200.0, 98.6, 37.0].average())
// Prints "648.9"
// 你可以在一個(gè)泛型 where 分句中包含多個(gè)要求來(lái)作為擴(kuò)展的一部分鲤遥,就如同你在其它地方寫(xiě)的泛型 where 分句一樣央渣。每一個(gè)需求用逗號(hào)分隔。