Chapters

Hide chapters

Swift Cookbook

Live Edition · Multiplatform · Swift · Editor agnostic

Use Tuple Shuffling in Swift
Written by Team Kodeco

In Swift, you can use tuple shuffling to rearrange the elements of a tuple. Tuple shuffling is useful for creating new tuples from existing ones, without having to access the elements individually.

Here’s an example of how to use tuple shuffling:

let tuple = (1, "apple")

let shuffledTuple = (tuple.1, tuple.0)
print(shuffledTuple)  // ("apple", 1)

You can also use tuple shuffling with named tuples:

let namedTuple = (number: 1, fruit: "apple")

let shuffledNamedTuple = (namedTuple.fruit, namedTuple.number)
print(shuffledNamedTuple)  // ("apple", 1)
© 2024 Kodeco Inc.