定义一个协议时,声明一个或多个关联类型作为协议定义的一部分将会非常有用。关联类型为协议中的某个类型提供了一个占位符名称,其代表的实际类型在协议被遵循时才会被指定。关联类型通过 associatedtype
关键字来指定。
关联类型的目的是让遵守这个协议的类、结构体或枚举可以用泛型去实现其方法。如果有多个类型的数据实现都能用到这个方法就会方便很多。
// 定义协议 Container
protocol Container {
// 关联类型Item代表的实际类型在协议被遵循时才会被指定
associatedtype Item
mutating func append(_ item: Item)
var count: Int { get }
subscript(i: Int) -> Item { get }
}
// 遵守协议 Container,指定关联类型为Int
struct IntStack: Container {
var items: [Int] = []
mutating func push(_ item: Item) {
items.append(item)
}
mutating func pop() -> Int {
return items.removeLast()
}
// Container 协议的实现部分
// typealias Item = Int // 可不写,swift会自己推断出来
mutating func append(_ item: Int) {
self.push(item)
}
var count: Int {
return items.count
}
subscript(i: Int) -> Int {
return items[i]
}
}
// 遵守协议 Container,指定关联类型为泛型Element
struct MyStack<Element>: Container {
var items: [Element] = []
mutating func push(_ item: Element) {
items.append(item)
}
mutating func pop() -> Element {
return items.removeLast()
}
// Container 协议的实现部分,这时关联类型Item就变成了泛型Element
mutating func append(_ item: Element) {
self.push(item)
}
var count: Int {
return items.count
}
subscript(i: Int) -> Element {
return items[i]
}
}