📖 Scenario: You are organizing a small fruit shop's inventory. Each fruit has a name and a price per kilogram. You want to group these two pieces of information together for easy handling.
🎯 Goal: Build a Swift program that uses tuples to group fruit names with their prices, then access and display this grouped information.
📋 What You'll Learn
Create a tuple with a fruit name and its price
Create a second tuple with another fruit name and price
Access tuple elements by name
Print the fruit name and price in a friendly message
💡 Why This Matters
🌍 Real World
Grouping related data like fruit name and price helps organize inventory information clearly and accessibly.
💼 Career
Understanding tuples is useful for developers to group small sets of related data without creating full custom types.
Progress0 / 4 steps
1
Create the first fruit tuple
Create a tuple called fruit1 with two named elements: name set to "Apple" and price set to 2.5.
Swift
Hint
Use let fruit1 = (name: "Apple", price: 2.5) to create the tuple with named elements.
2
Create the second fruit tuple
Create a tuple called fruit2 with two named elements: name set to "Banana" and price set to 1.2.
Swift
Hint
Use the same tuple syntax as before but with the new values.
3
Access tuple elements
Create two constants called firstFruitName and secondFruitPrice. Set firstFruitName to the name element of fruit1. Set secondFruitPrice to the price element of fruit2.
Swift
Hint
Use dot notation like fruit1.name to get the element from the tuple.
4
Print the fruit information
Print a message using print that says: "The first fruit is Apple and the second fruit costs 1.2 dollars per kg." Use the constants firstFruitName and secondFruitPrice inside the message with string interpolation.
Swift
Hint
Use print("The first fruit is \(firstFruitName) and the second fruit costs \(secondFruitPrice) dollars per kg.").
Practice
(1/5)
1. What is a tuple in Swift used for?
easy
A. Defining a function
B. Creating a new class type
C. Grouping multiple values into a single compound value
D. Declaring a constant
Solution
Step 1: Understand tuple purpose
Tuples combine multiple values into one grouped value without creating a new type.
Step 2: Compare with other options
Classes, functions, and constants serve different purposes unrelated to grouping multiple values simply.
Final Answer:
Grouping multiple values into a single compound value -> Option C
Quick Check:
Tuple = grouped values [OK]
Hint: Tuples bundle values together simply [OK]
Common Mistakes:
Confusing tuples with classes or structs
Thinking tuples define functions
Assuming tuples declare constants only
2. Which of the following is the correct syntax to declare a tuple with an Int and a String in Swift?
easy
A. let person: (Int, String) = (25, "Alice")
B. let person = Int, String = (25, "Alice")
C. let person = Int: 25, String: "Alice"
D. let person: Int, String = (25, "Alice")
Solution
Step 1: Check tuple declaration syntax
Correct tuple syntax uses parentheses with types inside, then assigns values in parentheses.
Step 2: Validate each option
let person: (Int, String) = (25, "Alice") correctly declares a tuple type and assigns matching values. Others have syntax errors or wrong format.
Final Answer:
let person: (Int, String) = (25, "Alice") -> Option A
A. let person = getPerson()
let personName = person[0]
let personAge = person[1]
B. let (personName, personAge) = getPerson()
C. let personName = getPerson().name
let personAge = getPerson().age
D. let personName, personAge = getPerson()
Solution
Step 1: Understand tuple unpacking
Swift allows unpacking tuple return values directly into variables using parentheses.
Step 2: Evaluate options
let (personName, personAge) = getPerson() correctly unpacks both values at once. let person = getPerson()
let personName = person[0]
let personAge = person[1] uses invalid indexing. let personName = getPerson().name
let personAge = getPerson().age attempts to access nonexistent labels. let personName, personAge = getPerson() has invalid syntax.
Final Answer:
let (personName, personAge) = getPerson() -> Option B
Quick Check:
Tuple unpacking uses let (a, b) = tuple [OK]
Hint: Unpack tuples with let (a, b) = function() [OK]
Common Mistakes:
Using square brackets to access tuple elements
Attempting to access tuple elements by nonexistent labels
Incorrect syntax for unpacking tuples
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.