| Start |
|---|
| What are Tuples? Tuples group multiple values into a single compound value. The values within a tuple can be of any type and do not have to be of the same type as each other. Tuples are created by grouping any amount of values: Also individual values can be named when the tuple is defined: They can also be named when being used as a variable and even have the ability to have optional values inside: |
| Decomposing into individual variables Tuples can be decomposed into individual variables with the following syntax: This syntax can be used regardless of if the tuple has unnamed properties: Specific properties can be ignored by using underscore ( |
| Tuples as the Return Value of Functions Functions can return tuples: If you assign parameter names, they can be used from the return value: |
| Using a typealias to name your tuple type Occasionally you may want to use the same tuple type in multiple places throughout your code. This can quickly get messy, especially if your tuple is complex: If you use a certain tuple type in more than one place, you can use the If you find yourself doing this too often, however, you should consider using a |
| Swapping values Tuples are useful to swap values between 2 (or more) variables without using temporary variables. Example with 2 variablesGiven 2 variables we can easily swap the values Result: Example with 4 variables |
| Tuples as Case in Switch Use tuples in a switch Or in combination with an Enum For example with Size Classes: RemarksTuples are considered value types. More info on tuples could be found in the documentation: developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html (opens new window) |
Read Article
https://develop.horje.com/learn/1434/reference