# Swift 泛型

Swift 語言提供「泛型」 的特性來編寫靈活和可重複使用功能類型。 泛型是用來避免重複而提供的抽象。Swift 標準庫是使用泛型建立的代碼庫。Swift 的'數組'和'字典'類型屬於泛型集合。數組和字典的幫助下，數組被定義爲持有「Int」的值，「字符串」值或任何其他類型。

func exchange(inout a: Int, inout b: Int) { let temp = a
a = b
b = temp } var numb1 = 100 var numb2 = 200 println("Before Swapping values are: \(numb1) and \(numb2)") exchange(&numb1, &numb2) println("After Swapping values are: \(numb1) and \(numb2)")

Before Swapping values are: 100 and 200
After Swapping values are: 200 and 100

## 泛型函數：類型參數

func exchange<T>(inout a: T, inout b: T) { let temp = a
a = b
b = temp } var numb1 = 100 var numb2 = 200 println("Before Swapping Int values are: \(numb1) and \(numb2)") exchange(&numb1, &numb2) println("After Swapping Int values are: \(numb1) and \(numb2)") var str1 = "Generics" var str2 = "Functions" println("Before Swapping String values are: \(str1) and \(str2)") exchange(&str1, &str2) println("After Swapping String values are: \(str1) and \(str2)")

Before Swapping Int values are: 100 and 200
After Swapping Int values are: 200 and 100
Before Swapping String values are: Generics and Functions
After Swapping String values are: Functions and Generics

## 泛型類型

struct TOS<T> { var items = [T]() mutating func push(item: T) { items.append(item) } mutating func pop() -> T { return items.removeLast() } } var tos = TOS<String>() tos.push("Swift") println(tos.items) tos.push("Generics") println(tos.items) tos.push("Type Parameters") println(tos.items) tos.push("Naming Type Parameters") println(tos.items) let deletetos = tos.pop()

[Swift]
[Swift, Generics]
[Swift, Generics, Type Parameters]
[Swift, Generics, Type Parameters, Naming Type Parameters]

## 擴展泛型類型

struct TOS<T> { var items = [T]() mutating func push(item: T) { items.append(item) } mutating func pop() -> T { return items.removeLast() } } var tos = TOS<String>() tos.push("Swift") println(tos.items) tos.push("Generics") println(tos.items) tos.push("Type Parameters") println(tos.items) tos.push("Naming Type Parameters") println(tos.items) extension TOS { var first: T? { return items.isEmpty ? nil : items[items.count - 1] } } if let first = tos.first { println("The top item on the stack is \(first).") }

[Swift]
[Swift, Generics]
[Swift, Generics, Type Parameters]
[Swift, Generics, Type Parameters, Naming Type Parameters]

## 類型約束

Swift 語言允許「類型約束」指定類型參數是否從一個特定的類繼承，或者確保協議一致性標準。

func exchange<T>(inout a: T, inout b: T) { let temp = a
a = b
b = temp } var numb1 = 100 var numb2 = 200 println("Before Swapping Int values are: \(numb1) and \(numb2)") exchange(&numb1, &numb2) println("After Swapping Int values are: \(numb1) and \(numb2)") var str1 = "Generics" var str2 = "Functions" println("Before Swapping String values are: \(str1) and \(str2)") exchange(&str1, &str2) println("After Swapping String values are: \(str1) and \(str2)")

Before Swapping Int values are: 100 and 200
After Swapping Int values are: 200 and 100
Before Swapping String values are: Generics and Functions
After Swapping String values are: Functions and Generics

## 關聯類型

Swift 允許相關類型，並可由關鍵字「typealias」協議定義內部聲明。

protocol Container { typealias ItemType mutating func append(item: ItemType) var count: Int { get } subscript(i: Int) -> ItemType { get } } struct TOS<T>: Container { // original Stack implementation var items = [T]() mutating func push(item: T) { items.append(item) } mutating func pop() -> T { return items.removeLast() } // conformance to the Container protocol mutating func append(item: T) { self.push(item) } var count: Int { return items.count } subscript(i: Int) -> T { return items[i] } } var tos = TOS<String>() tos.push("Swift") println(tos.items) tos.push("Generics") println(tos.items) tos.push("Type Parameters") println(tos.items) tos.push("Naming Type Parameters") println(tos.items)

[Swift]
[Swift, Generics]
[Swift, Generics, Type Parameters]
[Swift, Generics, Type Parameters, Naming Type Parameters]

## Where 子句

protocol Container { typealias ItemType mutating func append(item: ItemType) var count: Int { get } subscript(i: Int) -> ItemType { get } } struct Stack<T>: Container { // original Stack implementation var items = [T]() mutating func push(item: T) { items.append(item) } mutating func pop() -> T { return items.removeLast() } // conformance to the Container protocol mutating func append(item: T) { self.push(item) } var count: Int { return items.count } subscript(i: Int) -> T { return items[i] } } func allItemsMatch< C1: Container, C2: Container where C1.ItemType == C2.ItemType, C1.ItemType: Equatable> (someContainer: C1, anotherContainer: C2) -> Bool { // 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 } var tos = Stack<String>() tos.push("Swift") println(tos.items) tos.push("Generics") println(tos.items) tos.push("Where Clause") println(tos.items) var eos = ["Swift", "Generics", "Where Clause"] println(eos)

[Swift]
[Swift, Generics]
[Swift, Generics, Where Clause]
[Swift, Generics, Where Clause]