Swift – Access Values of Tuples using Labels

To access values of Tuples using labels in Swift, use dot operator.

The syntax to access values of a tuple using label is

aTuple.aLabel

Examples

In the following program, we initialize a Tuple x with values, where each value is given a label. We shall access the values of this Tuple using labels.

main.swift

var x = (a: "Hello", b: 35, c:true)
print(x.a)
print(x.b)
print(x.c)

Output

Swift - Access Values of Tuples using Labels

If only some of the values of a given Tuple are labeled, then only those can be accessed with labels. Rest of them could be accessed using index or other means.

main.swift

var x = (a: "Hello", 35, c:true)
print(x.a)
print(x.c)

Output

Swift - Access Values of Tuples using Labels
ADVERTISEMENT

Conclusion

In this Swift Tutorial, we have learned how to access values of a Tuple using labels, in Swift programming.