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)

extension TOS {
var first: T? {
return items.isEmpty? nil : items[items.count - 1 ]
}
}

if let first = tos.first {
println("The top item on the stack is \(first).")
}

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]

The top item on the stack is Naming Type Parameters.


Type Constraints


Swift language allows 'type constraints' to specify whether the type parameter inherits
from a specific class, or to ensure protocol conformance standard.

Free download pdf