Swift Tutorial - Tutorialspoint

(backadmin) #1

for each protocol requirement implementation within the extension is provided with its
own protocol access level.


Access Control for Generics


Generics allow the user to specify minimum access levels to access the type constraints
on its type parameters.


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

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

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]
Free download pdf