Swift中的泛型语法实践

先来简单的,

由浅入深嘛。



func swapTwoValue<T>(_ a: inout T, _ b: inout T) {
	let temporaryA = a
	a = b
	b = temporaryA
}

var someInt = 3
var anotherInt = 107
swapTwoValue(&someInt, &anotherInt)

var someString = "hello"
var anotherString = "world"
swapTwoValue(&someString, &anotherString)

print(someInt, someString)

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

var stackOfString = Stack<String>()
stackOfString.push("uno")
stackOfString.push("dos")
stackOfString.push("res")
stackOfString.push("cuatr")
stackOfString.push("bemo")

let fromTheTop = stackOfString.pop()

print(fromTheTop)



上一篇:用于部分迁移学习的加权对抗网络 | CVPR 2018论文解读


下一篇:Linux 进程管理的常用命令示例