Swift Tutorial - Tutorialspoint

(backadmin) #1
var tos = TOS<String>()

tos.push("Swift")
println(tos.items)

tos.push("Generics")
println(tos.items)

tos.push("Type Parameters")
println(tos.items)

tos.push("Naming Type Parameters")
println(tos.items)

let deletetos = tos.pop()

When we run the above program using playground, we get the following result:


[Swift]
[Swift, Generics]
[Swift, Generics, Type Parameters]
[Swift, Generics, Type Parameters, Naming Type Parameters]

Extending a Generic Type


Extending the stack property to know the top of the item is included with 'extension'
keyword.


struct TOS<T> {
var items = [T]()
mutating func push(item: T) {
items.append(item)
}

mutating func pop() - > T {
return items.removeLast()
}
Free download pdf