先来简单的,
由浅入深嘛。
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)