Chapters

Hide chapters

Swift Cookbook

Live Edition · Multiplatform · Swift · Editor agnostic

Use Named Tuples in Swift
Written by Team Kodeco

In Swift, you can use named tuples to give each element of a tuple a specific name. This makes it easier to access and understand the elements of the tuple.

Here’s an example of how to create a named tuple:

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

You can then access the elements of the named tuple using their names:

print(namedTuple.number)  // 1
print(namedTuple.fruit)  // "apple

You can also decompose a named tuple into separate variables or constants:

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