Swift - 泛型



Swift 提供了一個稱為“泛型”的特殊功能,用於編寫靈活且可重用的函式和型別,這些函式和型別可以很好地與任何其他型別一起使用。泛型用於避免重複和提供抽象。Swift 有一些使用泛型程式碼構建的標準庫。陣列和字典型別屬於泛型集合。

因此,我們可以建立一個儲存字串值的陣列,也可以建立一個儲存整數值的陣列。字典也是如此。

示例

func exchange(inout a: Int, inout b: Int) {
   let temp = a
   a = b
   b = temp
}

var numb1 = 100
var numb2 = 200

print("Before Swapping values are: \(numb1) and \(numb2)")
exchange(&numb1, &numb2)
print("After Swapping values are: \(numb1) and \(numb2)")

輸出

它將產生以下輸出:

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

泛型函式

泛型函式是可以用於訪問任何資料型別(如“Int”或“String”)同時保持型別安全的特殊函式。或者我們可以說泛型函式可以在不指定宣告時實際型別的情況下與不同型別一起工作。

示例

在下面的示例中,函式 exchange() 用於交換上述程式中描述的值,而 <T> 用作型別引數。第一次呼叫函式 exchange() 以返回“Int”值,第二次呼叫函式 exchange() 將返回“String”值。可以在尖括號內用逗號分隔多個引數型別。

func exchange<T>(_ a: inout T, _ b: inout T) {
   let temp = a
   a = b
   b = temp
}

var numb1 = 100
var numb2 = 200

print("Before Swapping Int values are: \(numb1) and \(numb2)")
exchange(&numb1, &numb2)
print("After Swapping Int values are: \(numb1) and \(numb2)")

var str1 = "Generics"
var str2 = "Functions"

print("Before Swapping String values are: \(str1) and \(str2)")
exchange(&str1, &str2)
print("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 提供 <T> 作為泛型型別引數名稱。但是,像陣列和字典這樣的型別引數也可以命名為鍵、值,以識別它們屬於“字典”型別。我們可以透過在尖括號內編寫多個型別引數名稱來提供多個型別引數,其中每個名稱都用逗號分隔。

示例

// Generic Types
struct TOS<T> {
   var items = [T]()
    
   mutating func push(item: T) {
      items.append(item)
   }
    
   mutating func pop() -> T? {
      return items.popLast()
   }
}

var tos = TOS<String>()
tos.push(item: "Swift 4")
print(tos.items)

tos.push(item: "Generics")
print(tos.items)

tos.push(item: "Type Parameters")
print(tos.items)

tos.push(item: "Naming Type Parameters")
print(tos.items)

if let deletetos = tos.pop() {
   print("Popped item: \(deletetos)")
} else {
   print("The stack is empty.")
}
輸出

它將產生以下輸出:

["Swift 4"]
["Swift 4", "Generics"]
["Swift 4", "Generics", "Type Parameters"]
["Swift 4", "Generics", "Type Parameters", "Naming Type Parameters"]
Popped item: Naming Type Parameters

泛型型別和擴充套件泛型型別

在 Swift 中,我們可以定義泛型型別來建立靈活且可重用的結構、類或列舉,這些結構、類或列舉可以輕鬆地與任何資料型別一起使用。此外,我們可以使用 extension 關鍵字擴充套件泛型型別的功能。

示例

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(item: "Swift 4")
print(tos.items)

tos.push(item: "Generics")
print(tos.items)

tos.push(item: "Type Parameters")
print(tos.items)

tos.push(item: "Naming Type Parameters")
print(tos.items)

extension TOS {
   var first: T? {
      return items.isEmpty ? nil : items[items.count - 1]
   }
}

if let first = tos.first {
   print("The top item on the stack is \(first).")
}

輸出

它將產生以下輸出:

["Swift 4"]
["Swift 4", "Generics"]
["Swift 4", "Generics", "Type Parameters"]
["Swift 4", "Generics", "Type Parameters", "Naming Type Parameters"]
The top item on the stack is Naming Type Parameters.

型別約束

Swift 允許“型別約束”來指定型別引數是否繼承自特定類,或確保協議一致性標準。我們可以將它們與類和協議一起使用以指定更復雜的約束。在建立自定義泛型型別時,我們可以建立自己的型別約束。

語法

以下是型別約束的語法:

Func functionName<T: className, U: protocolName>(variable1: T, variable2: U){
   // Function body
}

示例

// A generic function with a type constraint
func show<T: CustomStringConvertible>(item: T) {
   print(item.description)
}

let str = "Welcome Swift"
let number = 22

show(item: str) 
show(item: number)    

輸出

它將產生以下輸出:

Welcome Swift
22

Where 子句

型別約束使使用者能夠定義與泛型函式或型別關聯的型別引數的要求。為了定義關聯型別的要求,'where' 子句被宣告為型別引數列表的一部分。'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<T> 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 4")
print(tos.items)

tos.push("Generics")
print(tos.items)

tos.push("Where Clause")
print(tos.items)

var eos = ["Swift 4", "Generics", "Where Clause"]
print(eos)

輸出

它將產生以下輸出:

[Swift 4]
[Swift 4, Generics]
[Swift 4, Generics, Where Clause]
[Swift 4, Generics, Where Clause]
廣告