当前位置:K88软件开发文章中心编程语言APP编程Swift01 → 文章内容

Swift 泛型

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-12 6:31:34

其元素。这三个功能都符合 Container 协议的要求,所以你只需简单地声明 Array 采纳该协议就可以扩展 Array。以下实例创建一个空扩展即可:extension Array: Container {}类型约束类型约束指定了一个必须继承自指定类的类型参数,或者遵循一个特定的协议或协议构成。类型约束语法你可以写一个在一个类型参数名后面的类型约束,通过冒号分割,来作为类型参数链的一部分。这种作用于泛型函数的类型约束的基础语法如下所示(和泛型类型的语法相同):func someFunction<T: SomeClass, U: SomeProtocol>(someT: T, someU: U) { // 这里是泛型函数的函数体部分}上面这个函数有两个类型参数。第一个类型参数 T,有一个要求 T 必须是 SomeClass 子类的类型约束;第二个类型参数 U,有一个要求 U 必须符合 SomeProtocol 协议的类型约束。实例泛型// 非泛型函数,查找指定字符串在数组中的索引func findIndex(ofString valueToFind: String, in array: [String]) -> Int? {for (index, value) in array.enumerated() {if value == valueToFind {// 找到返回索引值return index}}return nil}let strings = ["google", "weibo", "taobao", "k88", "facebook"]if let foundIndex = findIndex(ofString: "k88", in: strings) {print("k88 的索引为 \(foundIndex)")}索引下标从 0 开始。以上程序执行输出结果为:k88 的索引为 3关联类Swift 中使用 associatedtype 关键字来设置关联类型实例。下面例子定义了一个 Container 协议,该协议定义了一个关联类型 ItemType。Container 协议只指定了三个任何遵从 Container 协议的类型必须提供的功能。遵从协议的类型在满足这三个条件的情况下也可以提供其他额外的功能。// Container 协议protocol Container { associatedtype ItemType // 添加一个新元素到容器里 mutating func append(_ item: ItemType) // 获取容器中元素的数 var count: Int { get } // 通过索引值类型为 Int 的下标检索到容器中的每一个元素 subscript(i: Int) -> ItemType { get }}// Stack 结构体遵从 Container 协议struct Stack<Element>: Container { // Stack<Element> 的原始实现部分 var items = [Element]() mutating func push(_ item: Element) { items.append(item) } mutating func pop() -> Element { return items.removeLast() } // Container 协议的实现部分 mutating func append(_ item: Element) { self.push(item) } var count: Int { return items.count } subscript(i: Int) -> Element { return items[i] }}var tos = Stack<String>()tos.push("google")tos.push("k88")tos.push("taobao")// 元素列表print(tos.items)// 元素个数print( tos.count)以上程序执行输出结果为:["google", "k88", "taobao"]3Where 语句类型约束能够确保类型符合泛型函数或类的定义约束。你可以在参数列表中通过where语句定义参数的约束。你可以写一个where语句,紧跟在在类型参数列表后面,where语句后跟一个或者多个针对关联类型的约束,以及(或)一个或多个类型和关联类型间的等价(equality)关系。实例下面的例子定义了一个名为allItemsMatch的泛型函数,用来检查两个Container实例是否包含相同顺序的相同元素。如果所有的元素能够匹配,那么返回 true,反之则返回 false。泛型// Container 协议protocol Container {associatedtype ItemType// 添加一个新元素到容器里mutating func append(_ item: ItemType)// 获取容器中元素的数var count: Int { get }// 通过索引值类型为 Int 的下标检索到容器中的每一个元素subscript(i: Int) -> ItemType { get }}// // 遵循Container协议的泛型TOS类型struct Stack<Element>: Container {// Stack<Element> 的原始实现部分var items = [Element]()mutating func push(_ item: Element) {items.append(item)}mutating func pop() -> Element {return items.removeLast()}// Container 协议的实现部分mutating func append(_ item: Element) {self.push(item)}var count: Int {return items.count}subscript(i: Int) -> Element {return items[i]}}// 扩展,将 Array 当作 Container 来使用extension Array: Container {}func allItemsMatch<C1: Container, C2: Container>(_ someContainer: C1, _ anotherContainer: C2) -> Boolwhere C1.ItemType == C2.ItemType, C1.ItemType: Equatable {// 检查两个容器含有相同数量的元素if someContainer.count != anotherContainer.count {return false}// 检查每一对元素是否相等for i in 0..<someContainer.count {if someContainer[i] != anotherContainer[i] {return false}}// 所有元素都匹配,返回 truereturn true}var tos = Stack<String>()tos.push("google")tos.push("k88")tos.push("taobao")var aos = ["google", "k88", "taobao"]if allItemsMatch(tos, aos) {print("匹配所有元素")} else {print("元素不匹配")}以上程序执行输出结果为:匹配所有元素

上一页  [1] [2] 


Swift 泛型