1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
| /// 泛型函数 var n1 = 10 var n2 = 20 func swapValues<T>(_ a: inout T, _ b: inout T) { (a, b) = (b, a) } swapValues(&n1, &n2) print(n1) print(n2)
/// 泛型函数赋值给变量 var fn: (inout Int, inout Int) -> () = swapValues
/// 泛型类型 class Stack<E> { var elements: [E] = [] init(firstElement: E) { elements.append(firstElement) } func push(_ element: E) { elements.append(element) } func pop() -> E { return elements.removeLast() } func top() -> E? { return elements.last } func size() -> Int { return elements.count } }
var intStack = Stack(firstElement: 10) //var intStack = Stack<Int>() //var stringStack = Stack<String>() //var anyStack = Stack<Any>()
/// 泛型继承 class SubStack<E>: Stack<E> { }
/// 泛型结构体 struct StackStruct<E> { var elements: [E] = [] mutating func push(_ element: E) { elements.append(element) } mutating func pop() -> E { return elements.removeLast() } func top() -> E? { return elements.last } func size() -> Int { return elements.count } }
/// 泛型枚举 enum Score<T> { case point(T) case grade(String) }
let score0 = Score<Int>.point(10) let score1 = Score.point(10.0) let score2 = Score<Int>.grade("A")
/// 泛型本质, 根据类型元信息操作
/// 关联类型 /// 作用: 给协议中用到的类型定义一个占位名称 /// 协议可以有多个关联类型 protocol Stackable { associatedtype Element mutating func push(_ element: Element) mutating func pop() -> Element func top() -> Element? func size() -> Int }
class StringStack: Stackable { var elements: [String] = [] func push(_ element: String) { elements.append(element) } func pop() -> String { return elements.removeLast() } func top() -> String? { return elements.last } func size() -> Int { return elements.count } }
/// 类型约束 protocol Runnable { } class Person { } func swapValues<T: Person & Runnable>(_ a: inout T, _ b: inout T) { (a, b) = (b, a) }
protocol Stackable1 { associatedtype Element: Equatable }
class Stack1<E: Equatable>: Stackable1 { typealias Element = E }
func equal<S1: Stackable1, S2: Stackable1>(_ s1: S1, _ s2: S2) -> Bool where S1.Element == S2.Element, S1.Element: Hashable { return false }
var s1 = Stack1<Int>() var s2 = Stack1<Int>() var s3 = Stack1<String>()
equal(s1, s2) //equal(s1, s3) //error, S1.Element == S2.Element
/// 协议类型注意点 protocol Runnable1 { associatedtype Speed var speed: Speed { get } }
class Person1: Runnable1 { var speed: Double { return 0.0 } } class Car1: Runnable1 { var speed: Int { return 0 } }
//func get(_ type: Int) -> Runnable1 { // if type == 0 { // return Person1() // } else { // return Car1() // } //}
func get(_ type: Int) -> some Runnable1 { return Car1() }
var r1 = get(0) //Runnable1 var r2 = get(1) //Runnable1
/// 不透明类型的限制 (带有关联类型的协议) /// 1. 只能返回一种明确类型, 这样才能确定关联类型 /// 2. some Runnable1 屏蔽一些内部实现细节, 只让外界知道是Runnable1
|