- Swift 教程
- Swift - 首頁
- Swift - 概述
- Swift - 環境
- Swift - 基本語法
- Swift - 變數
- Swift - 常量
- Swift - 字面量
- Swift - 註釋
- Swift 運算子
- Swift - 運算子
- Swift - 算術運算子
- Swift - 比較運算子
- Swift - 邏輯運算子
- Swift - 賦值運算子
- Swift - 位運算子
- Swift - 其他運算子
- Swift 高階運算子
- Swift - 運算子過載
- Swift - 算術溢位運算子
- Swift - 恆等運算子
- Swift - 範圍運算子
- Swift 資料型別
- Swift - 資料型別
- Swift - 整數
- Swift - 浮點數
- Swift - Double
- Swift - 布林值
- Swift - 字串
- Swift - 字元
- Swift - 類型別名
- Swift - 可選型別
- Swift - 元組
- Swift - 斷言和前提條件
- Swift 控制流
- Swift - 決策控制
- Swift - if 語句
- Swift - if...else if...else 語句
- Swift - if-else 語句
- Swift - 巢狀 if 語句
- Swift - switch 語句
- Swift - 迴圈
- Swift - for in 迴圈
- Swift - while 迴圈
- Swift - repeat...while 迴圈
- Swift - continue 語句
- Swift - break 語句
- Swift - fall through 語句
- Swift 集合
- Swift - 陣列
- Swift - 集合
- Swift - 字典
- Swift 函式
- Swift - 函式
- Swift - 巢狀函式
- Swift - 函式過載
- Swift - 遞迴
- Swift - 高階函式
- Swift 閉包
- Swift - 閉包
- Swift - 逃逸和非逃逸閉包
- Swift - 自動閉包
- Swift 面向物件程式設計
- Swift - 列舉
- Swift - 結構體
- Swift - 類
- Swift - 屬性
- Swift - 方法
- Swift - 下標
- Swift - 繼承
- Swift - 重寫
- Swift - 初始化
- Swift - 析構
- Swift 高階
- Swift - ARC 概述
- Swift - 可選鏈
- Swift - 錯誤處理
- Swift - 併發
- Swift - 型別轉換
- Swift - 巢狀型別
- Swift - 擴充套件
- Swift - 協議
- Swift - 泛型
- Swift - 訪問控制
- Swift - 函式與方法
- Swift - SwiftyJSON
- Swift - 單例類
- Swift 隨機數
- Swift 不透明型別和裝箱型別
- Swift 有用資源
- Swift - 線上編譯
- Swift - 快速指南
- Swift - 有用資源
- Swift - 討論
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]