泛型(Generics)
1、泛型可以將類型參數(shù)化娶视,提高代碼復(fù)用率,減少代碼量
func swapValue<T>(_ a: inout T, _ b: inout T) -> Void {
(a, b) = (b, a)
}
var fn: (inout Int, inout Int) -> () = swapValue
棧
class Stack<E> {
var elements = [E]()
func push(_ element: E) -> Void {
elements.append(element)
}
func pop() -> E {
elements.removeLast()
}
func top() -> E {
elements.last!
}
func size() -> Int {
elements.count
}
}
var intStack = Stack<Int>()
var stringStack = Stack<String>()
var anyStack = Stack<Any>()
class SubStack<E>: Stack<E> {
}
struct Stack<E> {
var elements = [E]()
mutating func push(_ element: E) {
elements.append(element)
}
mutating func pop() -> E {
elements.removeLast()
}
func top() -> E {
elements.last!
}
func size() -> Int {
elements.count
}
}
匯編分析泛型的實(shí)現(xiàn)原理
通過(guò)匯編我們可以看到兩次調(diào)用的swapValues方法的地址是相同的菱皆,也就是說(shuō)是相同的方法汤求,匯編中有關(guān)于metadata的信息,所以推測(cè)Swift中的泛型是通過(guò)對(duì)元數(shù)據(jù)的處理實(shí)現(xiàn)的肴楷。
關(guān)聯(lián)類型(Associated Type)
1水由、關(guān)聯(lián)類型的作用:給協(xié)議中用到的類型定義一個(gè)占位名稱
2、協(xié)議中可以擁有多個(gè)關(guān)聯(lián)類型
protocol Stackable {
associatedtype Element
mutating func push(_ element: Element)
mutating func pop() -> Element
func top() -> Element
func size() -> Int
}
class StringStack: Stackable {
// typealias Element = String 可寫(xiě)可不寫(xiě)
var elements = [String]()
func push(_ element: String) -> Void {
elements.append(element)
}
func pop() -> String {
elements.removeLast()
}
func top() -> String {
elements.last!
}
func size() -> Int {
elements.count
}
}
類型約束
protocol Runnable {}
class Person {}
func swapValues<T: Person & Runnable>(_ a: inout T, _ b: inout T) -> Void {
(a, b) = (b, a)
}
更多的約束
func equal<S1: Stackable, S2: Stackable>(_ s1: S1, _ s2: S2) -> Bool
where S1.Element == S2.Element, S1.Element: Hashable
{
return false
}
協(xié)議類型的注意點(diǎn)
protocol Runnable {}
class Person: Runnable {}
class Car: Runnable {}
func getObject(_ type: Int) -> Runnable {
if type == 0 {
return Person()
}
return Car()
}
var r1 = getObject(0)
var r2 = getObject(1)
1赛蔫、如果協(xié)議中有associatedtype砂客,那么會(huì)有類型識(shí)別不了的問(wèn)題,以下代碼會(huì)報(bào)錯(cuò)
protocol Runnable {
associatedtype Speed
var speed: Speed {
get
}
}
class Person: Runnable {
var speed: Double {
0.0
}
}
class Car: Runnable {
var speed: Int {
0
}
}
func getObject(_ type: Int) -> Runnable {
if type == 0 {
return Person()
}
return Car()
}
泛型解決
解決方案1:使用泛型
protocol Runnable {
associatedtype Speed
var speed: Speed {
get
}
}
class Person: Runnable {
var speed: Double {
0.0
}
}
class Car: Runnable {
var speed: Int {
0
}
}
func getObject<T: Runnable>(_ type: Int) -> T {
if type == 0 {
return Person() as! T
}
return Car() as! T
}
var r1: Person = getObject(0)
var r2: Car = getObject(1)
不透明類型(Opaque Type)
1呵恢、解決方案2:使用some關(guān)鍵字聲明一個(gè)不透明類型
func getObject(_ type: Int) -> some Runnable {
return Car()
}
2鞠值、some限制只能返回一種類型
some
1、some除了用在返回值類型上渗钉,一般還可以用在屬性類型上
protocol Runnable {
associatedtype Speed
}
class Dog: Runnable {
typealias Speed = Double
}
class Person {
var pet: some Runnable {
return Dog()
}
}