📖 Scenario: You are learning how to use Swift's REPL (Read-Eval-Print Loop) and Playgrounds to quickly test and run Swift code snippets. This helps you experiment with Swift code in a simple and interactive way, just like trying out ideas on paper before writing a full program.
🎯 Goal: Build a simple Swift program step-by-step using REPL or Playgrounds. You will create variables, configure a helper value, write a loop to process data, and finally print the result. This will help you get comfortable with Swift syntax and the interactive coding environment.
📋 What You'll Learn
Create a dictionary with exact keys and values
Add a configuration variable with a specific value
Use a for loop with exact variable names to process the dictionary
Print the final result exactly as specified
💡 Why This Matters
🌍 Real World
Filtering data based on conditions is common in apps, like showing only available items or filtering search results.
💼 Career
Understanding how to quickly test and run Swift code in REPL or Playgrounds is useful for developers to prototype ideas and debug code efficiently.
Progress0 / 4 steps
1
Create a dictionary of fruits and their quantities
Create a dictionary called fruits with these exact entries: "apple": 3, "banana": 5, "orange": 2
Swift
Hint
Use square brackets to create a dictionary. Use colons to separate keys and values.
2
Add a minimum quantity threshold
Create a variable called minQuantity and set it to 3
Swift
Hint
Use var to create a variable and assign the number 3.
3
Filter fruits with quantity at least the threshold
Create a variable called filteredFruits and use a for loop with variables fruit and quantity to iterate over fruits. Add fruits with quantity greater than or equal to minQuantity to filteredFruits dictionary.
Swift
Hint
Start with an empty dictionary. Use a for loop to check each fruit's quantity. Add only those that meet the threshold.
4
Print the filtered fruits dictionary
Write print(filteredFruits) to display the filtered fruits dictionary
Swift
Hint
Use the print function to show the dictionary. The output should only include fruits with quantity 3 or more.
Practice
(1/5)
1. What is the main purpose of Swift Playgrounds?
easy
A. To provide an interactive environment to write and test Swift code easily
B. To compile Swift code into an executable file
C. To manage Swift project dependencies
D. To deploy Swift apps to the App Store
Solution
Step 1: Understand what Playgrounds offer
Swift Playgrounds allow users to write and run Swift code interactively with immediate feedback.
Step 2: Compare options with Playgrounds' purpose
Options B, C, and D describe other development tasks, not the main purpose of Playgrounds.
Final Answer:
To provide an interactive environment to write and test Swift code easily -> Option A
Playgrounds allow writing functions and immediately running them with test inputs to see results.
Step 2: Evaluate other options
Write the function in a Swift file and compile it with Xcode without running compiles but does not run immediately; Use Swift REPL but avoid Playgrounds because it can't run functions is false because Playgrounds can run functions; Write the function in a text editor and run it later in the terminal delays testing.
Final Answer:
Write the function in Playgrounds and call it with test values to see results immediately -> Option D
Quick Check:
Playgrounds = quick function testing [OK]
Hint: Use Playgrounds for instant function testing [OK]
Common Mistakes:
Thinking Playgrounds can't run functions
Confusing compiling with running
Delaying testing by using external editors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.