乐闻世界logo
搜索文章和话题

Swift 中的泛型是什么?如何使用泛型?

2月21日 15:09

Swift 中的泛型是什么?如何使用泛型?什么是泛型约束和关联类型?

Swift 中的泛型是一种强大的特性,允许你编写灵活、可重用的代码,同时保持类型安全。泛型使你能够编写适用于多种类型的函数和类型,而不需要为每种类型重复编写代码。

泛型的基本概念:

  • 泛型允许你使用占位符类型名称(如 T)来表示类型
  • 编译器会在使用时推断具体的类型
  • 提供类型安全,避免运行时类型错误
  • 减少代码重复,提高代码复用性

泛型函数:

swift
// 泛型函数示例 func swapValues<T>(_ a: inout T, _ b: inout T) { let temp = a a = b b = temp } // 使用泛型函数 var x = 10 var y = 20 swapValues(&x, &y)

泛型类型:

swift
// 泛型结构体 struct Stack<Element> { private var items: [Element] = [] mutating func push(_ item: Element) { items.append(item) } mutating func pop() -> Element? { return items.popLast() } } // 使用泛型类型 var intStack = Stack<Int>() intStack.push(1)

泛型约束:

  • 限制泛型类型必须遵循特定协议
  • 使用 where 子句添加更复杂的约束
  • 常见约束类型:
    • T: Equatable - 类型必须遵循 Equatable 协议
    • T: Comparable - 类型必须遵循 Comparable 协议
    • T: SomeProtocol - 类型必须遵循指定协议
    • T: AnyObject - 类型必须是类类型
swift
// 泛型约束示例 func findFirstIndex<T: Equatable>(of value: T, in array: [T]) -> Int? { for (index, item) in array.enumerated() { if item == value { return index } } return nil } // 使用 where 子句 func allItemsMatch<C1: Container, C2: Container>(_ container1: C1, _ container2: C2) -> Bool where C1.Item == C2.Item, C1.Item: Equatable { return true }

关联类型:

  • 在协议中定义的占位符类型名称
  • 使用 associatedtype 关键字声明
  • 允许协议定义灵活的类型要求
  • 在遵循协议时指定具体类型
swift
// 带有关联类型的协议 protocol Container { associatedtype Item mutating func append(_ item: Item) var count: Int { get } subscript(i: Int) -> Item { get } } // 遵循协议并指定关联类型 struct IntStack: Container { typealias Item = Int private var items: [Int] = [] mutating func append(_ item: Int) { items.append(item) } var count: Int { return items.count } subscript(i: Int) -> Int { return items[i] } }

最佳实践:

  1. 合理使用泛型提高代码复用性
  2. 使用泛型约束确保类型安全
  3. 在协议中使用关联类型提高灵活性
  4. 避免过度使用泛型导致代码复杂
  5. 为泛型参数使用有意义的名称
标签:Swift