📖 Scenario: Imagine you want to create a simple Swift program that adds two numbers and shows the result. This project will help you understand how Swift code is written and then compiled into native code that your computer can run directly.
🎯 Goal: You will write a Swift program that adds two numbers, set up a variable for the sum, and print the result. This shows the basic steps Swift takes before turning your code into native machine instructions.
📋 What You'll Learn
Create two integer variables with exact values
Create a variable to hold the sum of these two numbers
Add the two numbers and store the result in the sum variable
Print the sum to the console
💡 Why This Matters
🌍 Real World
Adding numbers is a basic task in many apps, like calculators or financial software.
💼 Career
Understanding how Swift code turns into native code helps you write efficient apps for iPhones and Macs.
Progress0 / 4 steps
1
Create two integer variables
Create two integer variables called numberOne and numberTwo with values 7 and 5 respectively.
Swift
Hint
Use var to create variables and assign the numbers 7 and 5.
2
Create a variable to hold the sum
Create a variable called sum and set it to 0 to prepare for storing the addition result.
Swift
Hint
Initialize sum with 0 before adding the numbers.
3
Add the two numbers and store the result
Add numberOne and numberTwo and assign the result to the variable sum.
Swift
Hint
Use the + operator to add the two variables and store the result in sum.
4
Print the sum
Use print(sum) to display the value of sum on the console.
Swift
Hint
Use the print function to show the sum on the screen.
Practice
(1/5)
1. What does Swift compile your code into?
easy
A. Java bytecode
B. Web pages
C. Python scripts
D. Fast native programs for your device
Solution
Step 1: Understand Swift compilation output
Swift compiles code into native machine code that runs directly on the device's processor.
Step 2: Compare with other options
Java bytecode, Python scripts, and web pages are not outputs of Swift compilation.
Final Answer:
Fast native programs for your device -> Option D
Quick Check:
Swift compiles to native code = Fast native programs for your device [OK]
Hint: Swift compiles to native code, not scripts or bytecode [OK]
Common Mistakes:
Confusing Swift output with interpreted languages
Thinking Swift produces web code
Mixing Swift with Java or Python outputs
2. Which command is used to compile Swift code into native code?
easy
A. swiftc
B. javac
C. python
D. gcc
Solution
Step 1: Identify Swift compiler command
The command swiftc is the official Swift compiler used to compile Swift code.