swift - 范型

// 跟java 里的范型一样

func swapTwoInts(_ a: inout Int, _ b: inout Int) {
    let temporaryA = a
    a = b
    b = temporaryA
}
var someInt = 3
var anotherInt = 107
swapTwoInts(&someInt, &anotherInt)
print("someInt is now \(someInt), and anotherInt is now \(anotherInt)")
// Prints "someInt is now 107, and anotherInt is now 3"

//swapTwoInts(::) 函数很实用,但是它只能用于 Int 值。如果你想交换两个 String 值,或者两个 Double 值,你只能再写更多的函数,
//为了解决这个问题,引入范型

泛型函数语法

func swapTwoValues(_ a: inout T, _ b: inout T) {
    let temporaryA = a
    a = b
    b = temporaryA
}
// 对比
func swapTwoInts(_ a: inout Int, _ b: inout Int)
func swapTwoValues(_ a: inout T, _ b: inout T)

使用范型

var someInt = 3
var anotherInt = 107
swapTwoValues(&someInt, &anotherInt)
// someInt is now 107, and anotherInt is now 3
 
var someString = "hello"
var anotherString = "world"
swapTwoValues(&someString, &anotherString)
// someString is now "world", and anotherString is now "hello"

eg2:

struct IntStack {
    var items = [Int]()
    mutating func push(_ item: Int) {
        items.append(item)
    }
    mutating func pop() -> Int {
        return items.removeLast()
    }
}
struct Stack {
    var items = [Element]()
    mutating func push(_ item: Element) {
        items.append(item)
    }
    mutating func pop() -> Element {
        return items.removeLast()
    }
}
// using

var stackOfStrings = Stack()
stackOfStrings.push("uno")
stackOfStrings.push("dos")
stackOfStrings.push("tres")
stackOfStrings.push("cuatro")
// the stack now contains 4 strings

let fromTheTop = stackOfStrings.pop()
// fromTheTop is equal to "cuatro", and the stack now contains 3 strings

扩展一个泛型类型

struct Stack {
    var items = [Element]()
    mutating func push(_ item: Element) {
        items.append(item)
    }
    mutating func pop() -> Element {
        return items.removeLast()
    }
}

extension Stack {
    var topItem: Element? {
        return items.isEmpty ? nil : items[items.count - 1]
    }
}
if let topItem = stackOfStrings.topItem {
    print("The top item on the stack is \(topItem).")
}
// Prints "The top item on the stack is tres."

类型约束

语法
// 第一个类型形式参数, T ,有一个类型约束要求 T 是 SomeClass 的子类。
// 第二个类型形式参数, U ,有一个类型约束要求 U 遵循 SomeProtocol 协议。

func someFunction(someT: T, someU: U) {
    // function body goes here
}
类型约束的应用
func findIndex(ofString valueToFind: String, in array: [String]) -> Int? {
    for (index, value) in array.enumerated() {
        if value == valueToFind {
            return index
        }
    }
    return nil
}

let strings = ["cat", "dog", "llama", "parakeet", "terrapin"]
if let foundIndex = findIndex(ofString: "llama", in: strings) {
    print("The index of llama is \(foundIndex)")
}
// Prints "The index of llama is 2"

// using 范型

// 这里写出了一个叫做 findIndex(of:in:) 的函数,可能是你期望的 findIndex(ofString:in:) 函数的一个泛型版本。
// 注意,函数的返回值仍然是 Int? ,因为函数返回一个可选的索引数字,而不是数组里的一个可选的值。
func findIndex(of valueToFind: T, in array:[T]) -> Int? {
    for (index, value) in array.enumerated() {
        if value == valueToFind {
            return index
        }
    }
    return nil
}
/*
 这个函数没有像上面写的那样编译。问题在于相等检查,” if value == valueToFind “。Swift 中的类型不是每种都能用相等操作符( == )来比较的。如果你创建自己的类或者结构体去描述一个复杂的数据模型,比如说,对于那个类或结构体来说,”相等”的意义不是 Swift 能替你猜出来的。因此,不能保证这份代码可以用于所有 T 可以表示的类型,当你尝试编译这份代码时会提示一个相应的错误。
*/

// 修正
// Swift 标准库中定义了一个叫做 Equatable 的协议,要求遵循其协议的类型要实现相等操作符( == )和不等操作符( != ),用于比较该类型的任意两个值。所有Swift标准库中的类型自动支持 Equatable 协议。

func findIndex(of valueToFind: T, in array:[T]) -> Int? {
    for (index, value) in array.enumerated() {
        if value == valueToFind {
            return index
        }
    }
    return nil
}

let doubleIndex = findIndex(of: 9.3, in: [3.14159, 0.1, 0.25])
// doubleIndex is an optional Int with no value, because 9.3 is not in the array
let stringIndex = findIndex(of: "Andrea", in: ["Mike", "Malcolm", "Andrea"])
// stringIndex is an optional Int containing a value of 2

关联类型 (associatedtype) -- 抽象类型

protocol Container {
    associatedtype ItemType
    mutating func append(_ item: ItemType)
    var count: Int { get }
    subscript(i: Int) -> ItemType { get }
}
// IntStack 为了实现 Container 协议,指定了适用于 ItemType 的类型是 Int 类型。 typealias ItemType = Int 把 ItemType 抽象类型转换为了具体的 Int 类型。

struct IntStack: Container {
    // original IntStack implementation
    var items = [Int]()
    mutating func push(_ item: Int) {
        items.append(item)
    }
    mutating func pop() -> Int {
        return items.removeLast()
    }
    // conformance to the Container protocol
    typealias ItemType = Int
    mutating func append(_ item: Int) {
        self.push(item)
    }
    var count: Int {
        return items.count
    }
    subscript(i: Int) -> Int {
        return items[i]
    }
}

// 你也可以做一个遵循 Container 协议的泛型 Stack 类型:

struct Stack: Container {
    // original Stack implementation
    var items = [Element]()
    mutating func push(_ item: Element) {
        items.append(item)
    }
    mutating func pop() -> Element {
        return items.removeLast()
    }
    // conformance to the Container protocol
    mutating func append(_ item: Element) {
        self.push(item)
    }
    var count: Int {
        return items.count
    }
    subscript(i: Int) -> Element {
        return items[i]
    }
}
// 这次,类型形式参数 Element 用于 append(_:) 方法的 item 形式参数和下标的返回类型。因此,对于这个容器,Swift可以推断出 Element 是适用于 ItemType 的类型。
扩展现有类型来指定关联类型
// 你可以扩展一个现有类型使其遵循一个协议,如在扩展里添加协议遵循描述的一样。这包括一个带关联类型的协议。

/* 
Swift 的 Array 类型已经提供了 append(_:) 方法、 count 属性、用 Int 索引取出其元素的下标。这三个功能满足了 Container 协议的要求。这意味着你可以通过简单地声明 Array 采纳协议,扩展 Array 使其遵循 Container 协议。通过一个空的扩展实现,如使用扩展声明采纳协议:
*/

extension Array: Container {}

/* 
数组已有的 append(_:) 方法和下标使得Swift能为 ItemType 推断出合适的类型,就像上面的泛型 Stack 类型一样。定义这个扩展之后,你可以把任何 Array 当做一个 Container 使用。
*/
泛型Where语句
func allItemsMatch
    (_ someContainer: C1, _ anotherContainer: C2) -> Bool
    where C1.ItemType == C2.ItemType, C1.ItemType: Equatable {
        
        // 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..
  • C1 必须遵循 Container 协议(写作 C1: Container );
  • C2 也必须遵循 Container 协议(写作 C2: Container );
  • C1 的 ItemType 必须和 C2 的 ItemType 相同(写作 C1.ItemType == C2.ItemType );
  • C1 的 ItemType 必须遵循 Equatable 协议(写作 C1.ItemType: Equatable )。
    // using
var stackOfStrings = Stack()
stackOfStrings.push("uno")
stackOfStrings.push("dos")
stackOfStrings.push("tres")
 
var arrayOfStrings = ["uno", "dos", "tres"]
 
if allItemsMatch(stackOfStrings, arrayOfStrings) {
    print("All items match.")
} else {
    print("Not all items match.")
}
// Prints "All items match."

带有泛型 Where 分句的扩展

extension Stack where Element: Equatable {
    func isTop(_ item: Element) -> Bool {
        guard let topItem = items.last else {
            return false
        }
        return topItem == item
    }
}
if stackOfStrings.isTop("tres") {
    print("Top element is tres.")
} else {
    print("Top element is something else.")
}
// Prints "Top element is tres."
struct NotEquatable { }
var notEquatableStack = Stack()
let notEquatableValue = NotEquatable()
notEquatableStack.push(notEquatableValue)
notEquatableStack.isTop(notEquatableValue) // Error
// 你可以使用泛型 where 分句来扩展到一个协议。下面的栗子把先前的 Container 协议扩展添加了一个 startsWith(_:)  方法。
extension Container where Item: Equatable {
    func startsWith(_ item: Item) -> Bool {
        return count >= 1 && self[0] == item
    }
}
if [9, 9, 9].startsWith(42) {
    print("Starts with 42.")
} else {
    print("Starts with something else.")
}
// Prints "Starts with something else."
// 上边栗子中的泛型 where 分句要求 Item 遵循协议,但你同样可以写一个泛型 where 分句来要求 Item 为特定类型。比如:
extension Container where Item == Double {
    func average() -> Double {
        var sum = 0.0
        for index in 0..

// 你可以在一个泛型 where 分句中包含多个要求来作为扩展的一部分,就如同你在其它地方写的泛型 where 分句一样。每一个需求用逗号分隔。

你可能感兴趣的:(swift - 范型)