Swift – Update Element of a Tuple at Specific Index

To update element of a Tuple at specific index in Swift, access the element using dot operator and index of the required element, and assign the new value to this element.

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

</>
Copy
x.2 = new_value

Examples

In the following program, we take a Tuple x, and update the element at index=2.

main.swift

</>
Copy
var x = (true, 35, "apple")
x.2 = "banana"
print(x)

Output

(true, 35, "banana")
Program ended with exit code: 0

Conclusion

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