上一章Swift教程请查看:swift协议protocol
Swift语言提供了“泛型”的通用特性来编写灵活的、可重用的函数和类型,泛型用于避免重复和提供抽象。Swift标准库是用泛型代码构建的,例如’数组’和’字典’类型属于泛型集合,在数组和字典的帮助下,数组被定义为保存’Int’值和’String’值或任何其他类型。
func exchange(a: inout Int, b: inout Int) {
let temp = a
a = b
b = temp
}
var numb1 = 100
var numb2 = 200
print("交换值前: \(numb1) and \(numb2)")
exchange(a: &numb1, b: &numb2)
print("交换值后: \(numb1) and \(numb2)")
泛型函数:类型参数
通用函数可用于访问任何数据类型,如’Int’或’String’。
func exchange<T>(a: inout T, b: inout T) {
let temp = a
a = b
b = temp
}
var numb1 = 100
var numb2 = 200
print("交换前: \(numb1) and \(numb2)")
exchange(a: &numb1, b: &numb2)
print("交换后: \(numb1) and \(numb2)")
var str1 = "Generics"
var str2 = "Functions"
print("交换前: \(str1) and \(str2)")
exchange(a: &str1, b: &str2)
print("交换后: \(str1) and \(str2)")
函数exchange()用于交换上述程序中描述的值,<T>用作类型参数。第一次调用函数exchange()来返回“Int”值,第二次调用函数exchange()将返回“String”值。可以在逗号分隔的尖括号中包含多个参数类型。
将类型参数命名为用户定义的参数,以了解它所持有的类型参数的用途。Swift提供<T>作为泛型类型参数名,然而,数组和字典等类型参数也可以被命名为键、值,以确定它们属于“Dictionary”类型。
struct TOS<T> {
var items = [T]()
mutating func push(item: T) {
items.append(item)
}
mutating func pop() -> T {
return items.removeLast()
}
}
var tos = TOS<String>()
tos.push(item: "Swift")
print(tos.items)
tos.push(item: "泛型")
print(tos.items)
tos.push(item: "参数")
print(tos.items)
tos.push(item: "OOP")
print(tos.items)
let deletetos = tos.pop()
扩展泛型类型
扩展堆栈属性以了解项目的顶部包含在’extension’关键字中。
struct TOS<T> {
var items = [T]()
mutating func push(item: T) {
items.append(item)
}
mutating func pop() -> T {
return items.removeLast()
}
}
var tos = TOS<String>()
tos.push(item: "Swift")
print(tos.items)
tos.push(item: "泛型")
print(tos.items)
tos.push(item: "参数")
print(tos.items)
tos.push(item: "OOP")
print(tos.items)
extension TOS {
var first: T? {
return items.isEmpty ? nil : items[items.count - 1]
}
}
if let first = tos.first {
print("栈顶 \(first).")
}
类型约束
Swift语言允许“类型约束”来指定类型参数是否继承自特定的类,或者确保协议一致性标准。
func exchange<T>(a: inout T, b: inout T) {
let temp = a
a = b
b = temp
}
var numb1 = 100
var numb2 = 200
print("Before: \(numb1) and \(numb2)")
exchange(a: &numb1, b: &numb2)
print("After: \(numb1) and \(numb2)")
var str1 = "泛型"
var str2 = "Functions"
print("Before: \(str1) and \(str2)")
exchange(a: &str1, b: &str2)
print("After: \(str1) and \(str2)")
关联类型
Swift允许关键字“associatedtype”在协议定义中声明关联类型。
protocol Container {
associatedtype ItemType
mutating func append(item: ItemType)
var count: Int { get }
subscript(i: Int) -> ItemType { get }
}
struct TOS<T>: Container {
var items = [T]()
mutating func push(item: T) {
items.append(item)
}
mutating func pop() -> T {
return items.removeLast()
}
mutating func append(item: T) {
self.push(item: item)
}
var count: Int {
return items.count
}
subscript(i: Int) -> T {
return items[i]
}
}
var tos = TOS<String>()
tos.push(item: "Swift")
print(tos.items)
tos.push(item: "泛型")
print(tos.items)
tos.push(item: "参数")
print(tos.items)
tos.push(item: "OOP")
print(tos.items)
Where子句
类型约束使用户能够定义与泛型函数或类型关联的类型参数的需求。用于定义相关类型的需求,“where”子句声明为类型参数列表的一部分。“where”关键字被放在类型参数列表之后,紧接着是关联类型的约束、类型与关联类型之间的相等关系。
protocol Container {
associatedtype ItemType
mutating func append(item: ItemType)
var count: Int { get }
subscript(i: Int) -> ItemType { get }
}
struct Stack<T>: Container {
var items = [T]()
mutating func push(item: T) {
items.append(item)
}
mutating func pop() -> T {
return items.removeLast()
}
mutating func append(item: T) {
self.push(item: item)
}
var count: Int {
return items.count
}
subscript(i: Int) -> T {
return items[i]
}
}
func allItemsMatch<
C1: Container, C2: Container
where C1.ItemType == C2.ItemType, C1.ItemType: Equatable>
(someContainer: C1, anotherContainer: C2) -> Bool {
if someContainer.count != anotherContainer.count {
return false
}
for i in 0..<someContainer.count {
if someContainer[i] != anotherContainer[i] {
return false
}
}
return true
}
var tos = Stack<String>()
tos.push(item: "Swift")
print(tos.items)
tos.push(item: "泛型")
print(tos.items)
tos.push(item: "Where子句")
print(tos.items)
var eos = ["Swift", "泛型", "Where子句"]
print(eos)
评论前必须登录!
注册