class Stack<T> { | |
var items = T[]() | |
//Push method | |
func push(item: T) | |
{ | |
items.append(item) | |
} | |
//Pop method. | |
//If the array is not empty, then the last element pushed will be returned else nil. | |
func pop() -> T? | |
{ | |
var returnItem :T? | |
//If array is not empty | |
if items.count != 0 | |
{ | |
returnItem = items.removeLast() | |
} | |
return returnItem | |
} | |
//Prints out all the elements to the console | |
func traverse() | |
{ | |
println("---- Stack ----\n") | |
for item in items.reverse() | |
{ | |
println(item) | |
} | |
println("\n") | |
} | |
} |
In the above snippet, you can see, <T> is the additions, right next to class name, that makes it a generic type. You can make use Type T within the implementation just like any other type.
You can check the sample project here in Github.