Swift – Get Element of a Tuple at Specific Index

To get element of a Tuple at specific index in Swift, use dot operator and specify the index of the required value.

The code to get element at index=2 of a tuple x is

x.2

The above expression returns the value/element.

Examples

In the following program, we initialize a Tuple x with values, and get the element at index=2.

main.swift

var x = (true, 35, "apple")
var e = x.2
print("Element : \(e)")

Output

Element : apple
Program ended with exit code: 0
ADVERTISEMENT

Conclusion

In this Swift Tutorial, we have learned how to get the element of a Tuple at specific index, in Swift programming.