swift学习之泛型
使用泛型能让我们写出灵活的,可复用的函数和类型,这些函数和类型会根据我们定义的要求与任何类型一起使用。使用泛型我们不仅可以避免重复的代码而且能以更加清晰抽象的方式表达代码意图。
泛型是Swift最强大的特征之一,并且许多Swift的标准库都是使用泛型的代码编译的。
泛型解决的问题
///整形
func swapTwoInts(_ a:inout Int, _ b:inout Int) {
let temp = a
a = b;
b = temp
}
///字符串
func swapTwoStrs(_ a: inout String, _b: inout String) {
let temp = a
a = b
b = temp
}
///浮点型
func swapTwoDoubles(_ a:inout Double, _ b:inout Double) {
let temp = a
b = a
a = temp
}
上述三个函数,分别在交换对应类型的两个值。但回归函数的本质,其实这三个函数的主体是一样的,只不过他们分别接受Int
、Double
和String
三种类型作为函数的入参。 基于此编写一个函数,能交换任何类型的两个值,会更有用,也更灵活。
泛型函数
///泛型函数
func swapTwoValues<T>(_ a:inout T, _ b:inout T) {
let temp = a
a = b
b = temp
}
///调用
var a = "swift"
var b = "Generics"
swap(&a, &b)
print("a=\(a)","b=\(b)") ///结果:a=Generics b=swift
-
泛型函数中会使用
占位符类型
代替实际的类型名称,如Int
,String
或Double
等。本例中此占位类型的名称为T
,此占位符类型名称并未指定T
到底是什么,而是表示无论T
代表什么类型,a
和b
都必须具备相同的T
类型。每次调用swapTwoValues(_:_:)
函数时,Swift
都需要进行类型推断,确定代替T
使用的实际类型。 - 上边泛型函数的中“T”不是只能就是T,它并不起实际作用,仅仅是占位符类型名称。他可以是“TS”,“S”,"XS"等等,甚至可以是“Int”,“String”等等类型符;
///泛型函数 func swapTwoValues<TS>(_ a:inout TS, _ b:inout TS) { let temp = a a = b b = temp } ///调用 var a = "swift" var b = "Generics" swap(&a, &b) print("a=\(a)","b=\(b)") ///结果:a=Generics b=swift
-
泛型函数与非泛型函数的区别在于,编写泛型函数时,函数的名称后需要使用尖括号
< >
,并在其中指定占位符类型的名称:<T>
。<>
用以告诉Swift
函数T
是此函数定义的占位符类型名称。因为T
是一个占位符类型,所以Swift
不会查找T
的实际类型。 - 类型参数
泛型函数调用时,可以被函数实际类型代替的参数。在泛型函数名称后尖括号中指定并命名后,意味着指定了类型参数,我们便可以使用此类型参数来定义函数的参数,函数的返回值类型。当然也可以采用
<T,Q,...>
的形式定义多个类型参数。 - 命名类型参数
在大多数情况下,类型参数命名是具有描述性的,例如
Dictionary <Key,Value>
中的Key
、Value
以及Array <Element>
中的Element
,它向我们展示了类型参数与我们所使用的泛型类型或泛型函数之间的关系。但是,当它们之间没有有意义的关系时,通常会使用单个字母(例如T
,U
和V
)来命名它们。注意:请始终为类型参数提供驼峰式的大写名称(例如
T
和MyTypeParameter
),以表明它们是类型的占位符,而不是值。
泛型类型
除了泛型函数,Swift
还允许我们能定义自己的泛型类型,涵盖类,结构体,枚举类型,并可以与任何类型一起使用。和字典或数组相似。
接下来我们将定义一个栈的结构体类型,命名为Stack
,定义Stack
类型之前,我们需要知道栈结构的特点是:先入后出,后入先出。
- 1.定义只能存储特定类型的栈
///模拟栈数据结构 struct Stack { var items = [Int]() ///入栈 mutating func push(_ item:Int) { items.append(item) } ///出栈 mutating func pop(_ item:Int) { return items.removeLast(item) } } ///调用 var stack_int = Stack() stack_int.push(7) stack_int.push(3) stack_int.push(2) print(stack_int) ///打印:Stack(items: [7, 3, 2])
如若我需要存储其他类型呢?
- 2.定义泛型类型
Stack
///模拟栈数据结构 struct Stack<Element> { var items = [Element]() ///入栈 mutating func push(_ item:Element) { items.append(item) } ///出栈 mutating func pop(_ item:Element) { return items.removeLast(item as! Int) } } ///调用 ///Int类型 var stack_int = Stack<Int>() stack_int.push(7) stack_int.push(3) stack_int.push(2) print(stack_int) ///打印:Stack(items: [7, 3, 2]) ///String类型 var stack_string = Stack<String>() stack_string.push("Swift1.0") stack_string.push("Swift3.0") stack_string.push("Swift5.0") print(stack_string)///打印:Stack<String>(items: ["Swift1.0", "Swift3.0", "Swift5.0"])
注意:
-
泛型类型
Stack
具有一个称为Element
的类型参数,而不是Int
的实际类型。Element
是为此泛型类型定义的占位符类型,在结构体定义中的任何位置都可以使用Element
来引用未来调用时的实际类型。 - 与泛型函数与非泛型函数的区别一样,泛型类型和非泛型类型区别在于,编写泛型类型时,类型的名称后需要使用尖括号
< >
,并在其中指定占位符类型的名称:<T>
。<>
用以告诉Swift此类型中的
T
是此类型定义的占位符类型名称。因为T
是一个占位符类型,所以Swift
不会查找T
的实际类型。 -
泛型类型的类型参数的和泛型函数的类型参数一致。在创建类型实例时,泛型类型名称后尖括号中指定并命名后,意味着指定了类型参数。当然也可以采用
<T,Q,...>
的形式定义多个类型参数。///模拟栈数据结构 struct Stack<Element,Element2> { var items = [Element]() ///入栈 mutating func push(_ item:Element) { items.append(item) } ///出栈 mutating func pop(_ item:Element) { return items.removeLast(item as! Int) } }
泛型类型的扩展
当扩展一个泛型类型的时候,我们不需要提供类型参数的列表作为此扩展定义的一部分。因为,定义泛型类型时定义好的类型参数在其扩展中依旧时可用的。
extension Stack {
var topItem:Element? {
items.last
}
}
///调用
var stact_string = Stack<String>()
stact_string.push("Swift")
if let topItem = stact_string.topItem {
print(topItem) ///打印:Swift
}
类型约束
泛型函数和泛型类型虽然可以与任何类型一起使用,但是有时我们需要强制限制可以一起使用的类型,这个时候就需要使用类型约束。比如:Swift中Dictionary
的Key
便被约束为必须遵守hashable
协议。 类型约束:指定类型参数必须继承自特定的类、遵守某个协议或协议组合。
-
类型约束的语法
语法:参数类型定义时,参数名称后放置单独的类或协议约束,约束与 参数名称之间使用冒号
:
隔开。 注意:类型的约束条件只能为类或协议。func someFunction<T: SomeClass, U: SomeProtocol>(someT: T, someU: U) { // `T`约束为继承自`SomeClass`的类型 `U`约束为遵守`SomeProtocol` 协议的类型 }
-
类型约束的使用
func findIndex<T>(of valueToFind: T, in array:[T]) -> Int? { for (index, value) in array.enumerated() { if value == valueToFind { return index } } return nil }
上述代码编译时会出现出错:
Binary operator '==' cannot be applied to two 'T' operands(操作数)
。因为==
操作符在Swift
中不是所有类型都支持。比如,我们自定义的类型,只有只有实现了Swift
标准库定义的Equatable
协议,才能运用==
或!=
来比较该类型的任意的两个值。因此正确的写法是需要添加类型约束的:func findIndex<T : Equatable>(of valueToFind: T, in array:[T]) -> Int? { for (index, value) in array.enumerated() { if value == valueToFind { return index } } return nil }
类型关联
当我们定义协议时,有时声明一个或多个关联类型作为协议定义的一部分是很有用的。 关联类型的作用,主要提供某个类型的占位名称,然后作为协议的一部分去使用。关联类型的实际使用类型直到协议被实现时才会指定。关联类型使用关键字associatedtype
指定。
-
类型关联的使用
//定义协议使用类型关联 protocol Container { associatedtype Item mutating func append(_ item : Item) var count : Int{get} subscript(i:Int)->Item{get} } //定义整型Stack类型 struct IntStack : Container { var items = [Int]() ///入栈 mutating func push(_ item:Int){ items.append(item) } ///出栈 mutating func pop(_ item:Int) -> Int { return items.removeLast() } ///实现协议时,需要明确关联类型的实际类型 typealias Item = Int //!< ① mutating func append(_ item: Item) {//!< ①若不存在,此处可直接 Int push(item) } ///计算属性 var count: Int { items.count } ///下标 subscript(i: Int) -> Int { items[i] } }
Typealias Item = Int
是针对Container
协议的实现,将Item
的抽象类型转换为Int
的具体类型。基于Swift
的类型推断,通过append(_ :)
方法便可以推断出Item
的类型以及下标返回值的类型。 采用关联类型作为协议定义的一部分时,此协议也可以被泛型类型实现。struct Stack<Element> : Container { var items = [Element]() ///入栈 mutating func push(_ item:Element){ items.append(item) } ///出栈 mutating func pop(_ item:Element) -> Element { return items.removeLast() } ///实现协议 typealias Item = Element ///自动提示为`Element` mutating func append(_ item: Element) { push(item) } ///计算属性 var count: Int { items.count } ///下标 subscript(i: Int) -> Element { items[i] } }
-
扩展现有类型以指定关联类型
我们知道,当特定类型已经实现了协议的要求,但尚未声明该类型遵守协议。可以通过扩展声明该类型遵守此协议。当协议中定义了关联类型同样也是可以的。
比如:
Swift
的Array
类型已经提供了Container
协议中方法,属性要求的实现,完全匹配Container
协议要求。这意味着我们通过Array
的扩展声明Array
遵守Container
协议,并且Array
内部对于协议要求的实现可以推断出协议关联类型Item
的实际类型。extension Array : Container{} //扩展现有类型以指定关联类型?是否成功。 extension Array : Container{ func associateTypeOne(_:Item){} func associateTypeTwo(_:Element){} func associateTypeThree(_ : Self){}//实现协议时,Self都会与协议实现类型进行关联 }
值得注意的是:在我们定义这个扩展之后,
we can use any Array as a Container ?
实际上此处知识点还需自己探索一番。 若我们有一个具体未使用关联类型的协议Int_Container
protocol Int_Container { mutating func append(_ item : Int) var count : Int{get} subscript(i:Int)->Int{get} }-
1. 定义函数,参数为协议类型。
func testProtocolWithAssociateTypeOne(_ parameter : Container) { /*报错:Protocol 'Container' can only be used as a generic constraint because it has Self or associated type requirements*/ }
func testProtocolNoAssociatetype(_ parameter : Int_Container){ //编译成功 }
-
2.使用
is
或as
判断某个类型是否遵守特定协议let array : [Any] = [1,"ddd",3] if array is Container { /*报错:Protocol 'Container' can only be used as a generic constraint because it has Self or associated type requirements*/ print("遵守此协议") } else { print("不遵守此协议") } if array is Int_Container { print("遵守此协议") } else { print("不遵守此协议") }
上述
1
,2
的示例中,带有关联类型的协议,不管是作为函数的参数类型或对象的属性类型,还是单独判断某个类型是否遵守此协议,都会报错:Protocol 'Container' can only be used as a generic constraint because it has Self or associated type requirements
。编译器告诉我们Container
协议有Self
或关联类型的要求,因此它只能被用来作为泛型的约束。 关于Self
的提示:系统库为协议提供了Self
关联类型,默认指向了实现此协议的类型。//系统的`Equatable `协议 public protocol Equatable { static func == (lhs: Self, rhs: Self) -> Bool } //实际实现 class Person : Equatable { //默认关联`Self`到`Person` static func == (lhs: Person, rhs: Person) -> Bool { return lhs.name == rhs.name } var name : String? var age : String? }
若我们的
Int_Container
协议定义中使用了关联类型Self
,编译器依旧会报此错误。protocol Int_Container { mutating func append(_ item : Int) var count : Int{get} subscript(i:Int)->Int{get} static func testCompare(l:Self,r:Self)->Bool }
对比泛型和协议的关联类型:
- 泛型:使用占位符类型完成泛型类型方法的实现,泛型的实际类型由使用此泛型类型者指定。即:使用时指定实际类型。
- 关联类型:使用占位符类型完成协议方法的定义,关联类型的实际类型由实现此协议者指定,即:实现时指定实际类型。
关联类型的协议用作泛型的约束举例:
//① struct TempStruct<T:Container> { let title : String = "关联类型的协议用作泛型类型的约束:代替`T`的实际类型必须遵守`Container`协议" func showYourMagic(_ para : T) -> Void { print(para) } } //② func showYourMagic<T>(_ para : T) -> Void { print(para) } showYourMagic("展示魔法")
总结:带有关联类型的协议只能用作泛型的约束。
-
-
添加约束到关联类型
可以为协议中的关联类型添加类型约束,以要求符合条件的类型满足这些约束。
protocol Container { associatedtype Item : Equatable mutating func append(_ item : Item) var count : Int{get} subscript(i:Int)->Item{get} }
-
在关联类型的约束中使用协议
在关联类型的约束中使用协议,协议可以作为协议要求的一部分出现。(当前协议可作为关联类型的协议要求出现)。 以
Container
协议举例,定义协议SuffixableContainer
继承自Container
,实现功能:实现此协议类型的实例,需要截取它后缀一定长度,组成新的实例。//协议定义 //定义继承协议 protocol SuffixableContainer : Container { /*新构建的关联类型`suffix`约束条件有两个: 1.实现此协议时指定的`suffix`的类型必须是实现`SuffixableContainer`协议的类型 2.此`suffix`占位的容器类型的存储项类型`Item`必须与当前实现此协议的存储项保持一致。 */ associatedtype suffix : SuffixableContainer where suffix.Item == Item /*`item`关联类型的实际类型由泛型类型的占位类型决定。 此方法必须确保`String`类型的容器,截取的后缀,重组后的容器仍然是`String`类型的*/ func suffix(_ size : Int) -> suffix } //实现 extension Stack : SuffixableContainer { func suffix(_ size: Int) -> Stack { var result = Stack() for index in (count-size)..<count { result.append(self[index]) } return result } } //调用 var stack_int = Stack<Int>() stack_int.push(7) stack_int.push(3) stack_int.push(2) stack_int.append(4) let suffix_int = stack_int.suffix(3) print(stack_int,suffix_int)//3 2 4
上述示例,
SuffixableContainer
协议的关联类型suffix
使用了SuffixableContainer
协议进行约束。 基于suffix(_ : ) -> suffix
此方法必须确保String
类型的特定容器,截取的后缀,重组后的容器仍然是String
类型的此容器。解释一下关于关联类型suffix
的约束:- 实现此协议时指定的
suffix
的类型必须是实现SuffixableContainer
协议的类型。 - 此
suffix
占位的容器类型的存储项类型Item
必须与当前实现此协议类型(调用类型)的存储项保持一致。item
关联类型的实际类型由泛型类型的占位类型决定。
- 实现此协议时指定的
泛型的where
闭包
where
闭包能要求关联类型必须遵守某个特定的协议,或特定的类型参数与关联类型必须相等。where
闭包以where
关键字开始,后跟关联类型的约束或类型参数与关联类型之间的相等关系。我们可以在类型或函数主体的大括号前写一个通用的where
子句来设置我们的约束。 以匹配两个容器是否相等的功能举例来阐述。
func twoContainerIsEqual<C1:Container,C2:Container>(_ someContainer : C1 , _ anotherContainer : C2) -> Bool where C1.Item == C2.Item , C2.Item : Equatable { /*where闭包对于关联类型的约束:1.容器元素类型一致, 2.元素的类型遵守`Equatable`协议*/ if someContainer.count != anotherContainer.count { return false } for i in 0..<someContainer.count { if someContainer[i] != anotherContainer[i] { return false } } return true }
使用where
闭包扩展泛型
- 1.
where
闭包可以作为泛型扩展的一部分。
extension Stack where Element: Equatable { func isTop(_ item: Element) -> Bool { guard let topItem = items.last else { return false } return topItem == item } } //调用 struct NotEquatable { } var notEquatableStack = Stack<NotEquatable>() let notEquatableValue = NotEquatable() notEquatableStack.push(notEquatableValue) /* Error:Argument type 'NotEquatable' does not conform to expected type 'Equatable'*/ notEquatableStack.isTop(notEquatableValue)
- 2.
where
闭包可以作为协议扩展的一部分。
/* 协议通过扩展可以为遵守协议的类型提供方法,初始化,下 标和计算属性的实现。这一点允许我们为协议本身定义行 为,而不是基于遵守协议的每个类型 */ extension Container where Item: Equatable { //若`startsWith`函数名不与`container`中要求重名,则`startsWith`便是为遵守此协议的类型增加了新的方法。 func startsWith(_ item: Item) -> Bool { return count >= 1 && self[0] == item } }
- 3.
where
闭包,可以要求Container
协议Item
为特定类型。
extension Container where Item == Double { func average() -> Double { var sum = 0.0 for index in 0..<count { sum += self[index] } return sum / Double(count) } }
关联类型使用泛型 where
闭包。
关联类型上使用泛型where
子句。 例如,为Container
协议增加迭代器的功能。
protocol Container { associatedtype Item mutating func append(_ item: Item) var count: Int { get } subscript(i: Int) -> Item { get } associatedtype Iterator : IteratorProtocol where Iterator.Element == Item func makeIterator() -> Iterator } //构建迭代器 struct Iterator<T> : IteratorProtocol{ var stack : Stack<T> var count = 0 init(_ stack : Stack<T>) { self.stack = stack } typealias Element = T mutating func next() -> T? { let next = stack.count - 1 - count guard next >= 0 else { return nil } count += 1 return stack[next] } } //我们的泛型`Stack`需要实现`Sequence`协议 struct Stack<Element> : Container,Sequence { //container只能用作泛型约束。 var items = [Element]() mutating func push(_ item:Element){ items.append(item) } mutating func pop(_ item:Element) -> Element { return items.removeLast() } //实现协议 typealias Item = Element //自动提示为`Element` mutating func append(_ item: Element) { push(item) } var count: Int { items.count } subscript(i: Int) -> Element { items[i] } //迭代器的实现 typealias IteratorType = Iterator<Element> func makeIterator() -> IteratorType { return Iterator.init(self) } } //调用 var stack_int = Stack<Int>() stack_int.push(7) stack_int.push(3) stack_int.push(2) stack_int.append(4) for item in stack_int { print(item) } //输出: 4 2 3 7 复制代码
Iterator : IteratorProtocol where Iterator.Element == Item
要求Iterator
必须遍历与容器的元素具有相同类型的元素,而不管迭代器的类型。
泛型下标
下标可以泛型,也可以包括泛型where
子句,下标后的尖括号内写占位符类型名称,并在下标正文的左花括号前写泛型where
子句。
extension Container { subscript<Indices: Sequence>(indices: Indices) -> [Item] where Indices.Iterator.Element == Int { var result = [Item]() for index in indices { result.append(self[index]) } return result } }
Indices.Iterator.Element == Int
保证了序列中的索引与用于容器的索引具有相同的类型。即:意味着为索引参数传递的值是整数序列。