func swapTwoInts(a: inout Int,b:inout Int) {
let temporaryA = a
a = b
b = temporaryA
}
func swapTwoStrings(a:inout String,b:inout String) {
let temporaryA = a
a = b
b = temporaryA
}
func swapTwoDoubles(a:inout Double,b:inout Double) {
let temporaryA = a
a = b
b = temporaryA
}
var someInt = 3
var anotherInt = 107
swapTwoInts(a: &someInt, b: &anotherInt)
print("someInt : \(someInt) , anotherInt : \(anotherInt)")
func swapTwoValues<T>(a:inout T,b:inout T) {
let temporaryA = a
a = b
b = temporaryA
}
var someInt = 3
var anotherInt = 107
swapTwoValues(a: &someInt, b: &anotherInt)
print("someInt : \(someInt) , anotherInt: \(anotherInt)")
var someStr = "Hello"
var anotherStr = "World"
swapTwoValues(a: &someStr, b: &anotherStr)
print("someStr : \(someStr) , anotherStr: \(anotherStr)")
struct IntStack {
var items = [Int]()
mutating func push(item:Int) {
items.append(item)
}
mutating func pop() -> Int {
return items.removeLast()
}
}
struct Stack<Element> {
var items = [Element]()
mutating func push(item:Element) {
items.append(item)
}
mutating func push() -> Element {
return items.removeLast()
}
}
var stackOfStrings = Stack<String>()
stackOfStrings.push(item: "uno")
stackOfStrings.push(item: "dos")
stackOfStrings.push(item: "tres")
stackOfStrings.push(item: "cuatro")
let fromTheTop = stackOfStrings.push()
if let topItem = stackOfStrings.topItem {
print(topItem)
}
protocol Container {
associatedtype Item
mutating func append(item:Item)
var count:Int{get}
subscript(i:Int)->Item {get}
}
struct Stack<Element> : Container {
var items = [Element]()
var topItem:Element? {
return items.isEmpty ? nil : items.last
}
mutating func push(item:Element) {
items.append(item)
}
mutating func push() -> Element {
return items.removeLast()
}
var count: Int {
return items.count
}
mutating func append(item: Element) {
self.push(item: item)
}
subscript(i: Int) -> Element {
return items[i]
}
}