Swift学习笔记(十八)泛型

前端之家收集整理的这篇文章主要介绍了Swift学习笔记(十八)泛型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

泛型

泛型代码可以让你写出根据自我需求定义、适用于任何类型的,灵活且可重用的函数和类型

泛型所解决的问题

定义一个swapTwoInts方法,用于交换两个int类型的值

:swapTwoInts

  1. func swapTwoInts(inout a: Int,inout b: Int) {
  2. let temporaryA = a
  3. a = b
  4. b = temporaryA
  5. }

:swapTwoStrings

  1. func swapTwoStrings(inout a: String,inout b: String) {
  2. let temporaryA = a
  3. a = b
  4. b = temporaryA
  5. }

:swapTwoDoubles

  1. func swapTwoDoubles(inout a: Double,inout b: Double) {
  2. let temporaryA = a
  3. a = b
  4. b = temporaryA
  5. }

上述定义了三个方法swapTwoIntsswapTwoStringsswapTwoDoubles功能都是相同的(交换两个数的值),唯一不同的是传入的变量类型不同

泛型函数(inout关键字表示参数是地址)

  1. func swapTwoValues<T>(inout a: T,inout b: T) {
  2. let temporaryA = a
  3. a = b
  4. b = temporaryA
  5. }
  6. var someInt = 3
  7. var anotherInt = 107
  8. swapTwoValues(&someInt,&anotherInt)
  9. // someInt is now 107,and anotherInt is now 3
  10.  
  11. var someString = "hello"
  12. var anotherString = "world"
  13. swapTwoValues(&someString,&anotherString)
  14. // someString is now "world",and anotherString is now "hello"

在简单的情况下,泛型函数或泛型类型需要指定一个占位类型,通常用一单个字母T来命名类型参数。不过,你可以使用任何有效的标识符来作为类型参数名。

泛型类型

非泛型版本的栈

  1. struct IntStack {
  2. var items = [Int]()
  3. mutating func push(item: Int) {
  4. items.append(item)
  5. }
  6. mutating func pop() -> Int {
  7. return items.removeLast()
  8. }
  9. }

泛型版本的栈

  1. struct Stack<T> {
  2. var items = [T]()
  3. mutating func push(item: T) {
  4. items.append(item)
  5. }
  6. mutating func pop() -> T {
  7. return items.removeLast()
  8. }
  9. }

  1. var stackOfStrings = Stack<String>()
  2. stackOfStrings.push("uno")
  3. stackOfStrings.push("dos")
  4. stackOfStrings.push("tres")
  5. stackOfStrings.push("cuatro")
  6. // 现在栈已经有4个string了
  7. let fromTheTop = stackOfStrings.pop()
  8. // fromTheTop is equal to "cuatro",and the stack now contains 3 strings


泛型类型拓展

向泛型栈中添加了一个只读的计算属性topItem,这个属性返回一个可选类型T的值。若栈不为空,则返回栈顶元素

  1. extension Stack {
  2. var topItem: T? {
  3. return items.isEmpty ? nil : items[items.count - 1]
  4. }
  5. }

类型约束

swapTwoValues函数Stack类型可以作用于任何类型,不过,有的时候对使用在泛型函数和泛型类型上的类型强制约束为某种特定类型是非常有用的。类型约束指定了一个必须继承自指定类的类型参数,或者遵循一个特定的协议或协议构成。

类型约束语法

  1. func someFunction<T: SomeClass,U: SomeProtocol>(someT: T,someU: U) {
  2. //语法
  3. }

上面这个假定函数有两个类型参数。第一个类型参数T,有一个需要T必须是SomeClass子类的类型约束;第二个类型参数U,有一个需要U必须遵循SomeProtocol协议的类型约束。


类型约束行为

名为findStringIndex的非泛型函数,该函数功能是去查找包含一给定String值的数组

  1. func findStringIndex(array: [String],valueToFind: String) -> Int? {
  2. for (index,value) in enumerate(array) {
  3. if value == valueToFind {
  4. return index
  5. }
  6. }
  7. return nil
  8. }
  9. let strings = ["cat","dog","llama","parakeet","terrapin"]
  10. if let foundIndex = findStringIndex(strings,"llama") {
  11. println("The index of llama is \(foundIndex)")
  12. }
  13. // prints "The index of llama is 2"

Swift 标准库中定义了一个Equatable协议,该协议要求任何遵循的类型实现等式符(==)和不等符(!=)对任何两个该类型进行比较。所有的 Swift 标准类型自动支持Equatable协议。

  1. func findIndex<T: Equatable>(array: [T],valueToFind: T) -> Int? {
  2. for (index,value) in enumerate(array) {
  3. if value == valueToFind {
  4. return index
  5. }
  6. }
  7. return nil
  8. }
  9. //T: Equatable,也就意味着“任何T类型都遵循Equatable协议”。
  10.  
  11. let doubleIndex = findIndex([3.14159,0.1,0.25],9.3)
  12. // doubleIndex is an optional Int with no value,because 9.3 is not in the array
  13. let stringIndex = findIndex(["Mike","Malcolm","Andrea"],"Andrea")
  14. // stringIndex is an optional Int containing a value of 2

关联类型

当定义一个协议时,有的时候声明一个或多个关联类型作为协议定义的一部分是非常有用的。一个关联类型给定作用于协议部分的类型一个节点名(或别名)。作用于关联类型上实际类型是不需要指定的,直到该协议接受。关联类型被指定为typealias关键字

关联类型行为

  1. protocol Container {
  2. typealias ItemType
  3. mutating func append(item: ItemType)
  4. var count: Int { get }
  5. subscript(i: Int) -> ItemType { get }
  6. }

Container协议定义了三个任何容器必须支持的兼容要求:

一、必须可能通过append方法添加一个新item到容器里;

二、必须可能通过使用count属性获取容器里items数量,并返回一个Int值;

三、必须可能通过容器的Int索引值下标可以检索到每一个item

  1. struct EmpIntStack: Container {
  2. // original IntStack implementation
  3. var items = [Int]()
  4. mutating func push(item: Int) {
  5. items.append(item)
  6. }
  7. mutating func pop() -> Int {
  8. return items.removeLast()
  9. }
  10. // conformance to the Container protocol
  11. typealias ItemType = Int
  12. mutating func append(item: Int) {
  13. self.push(item)
  14. }
  15. var count: Int {
  16. return items.count
  17. }
  18. subscript(i: Int) -> Int {
  19. return items[i]
  20. }
  21. }
  1. <span class="s1">//IntStack</span><span class="s2">指定了</span><span class="s1">Container</span><span class="s2">的实现,适用的</span><span class="s1">ItemType</span><span class="s2">被用作</span><span class="s1">Int</span><span class="s2">类型。对于这个</span><span class="s1">Container</span><span class="s2">协议实现而言,定义</span><span class="s1">typealias ItemType = Int</span><span class="s2">,将抽象的</span><span class="s1">ItemType</span><span class="s2">类型转换为具体的</span><span class="s1">Int</span><span class="s2">类型。</span>

你也可以生成遵循 Container 协议的泛型 Stack 类型:

  1. struct EmpStack<T>: Container {
  2. // original Stack<T> implementation
  3. var items = [T]()
  4. mutating func push(item: T) {
  5. items.append(item)
  6. }
  7. mutating func pop() -> T {
  8. return items.removeLast()
  9. }
  10. // conformance to the Container protocol
  11. mutating func append(item: T) {
  12. self.push(item)
  13. }
  14. var count: Int {
  15. return items.count
  16. }
  17. subscript(i: Int) -> T {
  18. return items[i]
  19. }
  20. }
  21. //这个时候,占位类型参数T被用作append方法的item参数和下标的返回类型。Swift 因此可以推断出被用作这个特定容器的ItemType的T的合适类型

Where 语句

一个where语句使你能够要求一个关联类型遵循一个特定的协议,以及(或)那个特定的类型参数和关联类型可以是相同的。你可写一个where语句,通过紧随放置where关键字在类型参数队列后面,其后跟着一个或者多个针对关联类型的约束,以及(或)一个或多个类型和关联类型的等于关系。

下面的列子定义了一个名为allItemsMatch的泛型函数,用来检查是否两个Container单例包含具有相同顺序的相同元素。如果匹配到所有的元素,那么返回一个为trueBoolean值,反之,则相反。

  1. func allItemsMatch<C1: Container,C2: Container where C1.ItemType == C2.ItemType,C1.ItemType:Equatable>
  2. (someContainer: C1,anotherContainer: C2) -> Bool {
  3. // check that both containers contain the same number of items
  4. if someContainer.count != anotherContainer.count {
  5. return false
  6. }
  7. // check each pair of items to see if they are equivalent
  8. for i in 0...someContainer.count {
  9. if someContainer[i] != anotherContainer[i] {
  10. return false
  11. }
  12. }
  13. // all items match,so return true
  14. return true
  15. }

这个函数用了两个参数:someContaineranotherContainersomeContainer参数是类型C1anotherContainer参数是类型C2C1C2是容器的两个占位类型参数,决定了这个函数何时被调用

这个函数的类型参数列紧随在两个类型参数需求的后面:

C1必须遵循Container协议 (写作 C1: Container)

C2必须遵循Container协议 (写作 C2: Container)

C1ItemType同样是C2ItemType(写作 C1.ItemType == C2.ItemType)。

C1ItemType必须遵循Equatable协议 (写作 C1.ItemType: Equatable)

③和④要求被定义为一个where语句的一部分,写在关键字where后面,作为函数类型参数链的一部分。

  1. var empStackOfStrings = Stack<String>()
  2. empStackOfStrings.push("uno")
  3. empStackOfStrings.push("dos")
  4. empStackOfStrings.push("tres")
  5.  
  6. var arrayOfStrings = ["uno","dos","tres"]
  7.  
  8. //if allItemsMatch(empStackOfStrings,arrayOfStrings) {
  9. // println("All items match.")
  10. //} else {
  11. // println("Not all items match.")
  12. //}
  13. // prints "All items match."

猜你在找的Swift相关文章