0
0
Swiftprogramming~5 mins

Tuples for grouped values in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a tuple in Swift?
A tuple is a simple way to group multiple values into one compound value. Each value can be of any type, and the tuple can hold different types together.
Click to reveal answer
beginner
How do you create a tuple with a name and age in Swift?
You can create it like this: <pre>let person = (name: "Alice", age: 30)</pre> This groups the name and age together with labels.
Click to reveal answer
beginner
How do you access values inside a tuple?
You can access values by their position or by their label if named. For example:
person.name
or
person.0
.
Click to reveal answer
beginner
Can tuples hold different types of values?
Yes! Tuples can hold values of different types together, like a String and an Int in the same tuple.
Click to reveal answer
intermediate
What is a practical use of tuples in Swift?
Tuples are useful to return multiple values from a function or to group related values without creating a full struct or class.
Click to reveal answer
How do you declare a tuple with two values: an Int and a String?
Alet pair = <42, "Hello">
Blet pair = [42, "Hello"]
Clet pair = {42, "Hello"}
Dlet pair = (42, "Hello")
How do you access the second value in a tuple named 'data' without labels?
Adata(1)
Bdata[1]
Cdata.1
Ddata.second
Which of these is a valid tuple with named elements?
Alet t = {x: 10, y: 20}
Blet t = (x: 10, y: 20)
Clet t = [x: 10, y: 20]
Dlet t = <x: 10, y: 20>
Can a tuple hold a String, an Int, and a Bool together?
AYes
BNo
COnly if all are the same type
DOnly if converted to String
What is a common use of tuples in Swift functions?
ATo return multiple values
BTo create classes
CTo declare variables
DTo loop through arrays
Explain what a tuple is and how you can use it to group values in Swift.
Think about how you can put different things together in one box.
You got /4 concepts.
    Describe how tuples can help when returning multiple values from a function.
    Imagine a function giving you more than one answer at once.
    You got /4 concepts.