Swift Programming Basics
# CHAPTER 3
Swift Programming Basics
1. Introduction
Before you can build complex user interfaces or fetch data from the internet, you must learn the vocabulary and grammar of the Swift language. Swift was designed by Apple to be incredibly clean, safe, and modern. Unlike older languages, it eliminates unnecessary semicolons and complex syntax, making it read almost like plain English. In this chapter, we will master Swift Programming Basics. We will explore how to store data in memory, write clean code using comments, and output information to the console using print statements.2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the core syntax rules of Swift.
- Create single-line and multi-line comments.
-
Utilize the
print()function to debug code.
- Understand the concept of Variables and Constants at a high level.
- Work with basic data forms: Strings, Numbers, and Booleans.
3. Swift Syntax and Semicolons
If you have used JavaScript, Java, or C++, you are used to putting a semicolon; at the end of every line.
In Swift, semicolons are not required. You simply press "Enter" to start a new line.
4. Comments in Swift
Comments are notes you leave in your code for yourself or other humans. The compiler completely ignores them.5. The Print Statement
When writing code, you frequently need to check what the computer is thinking. You do this by asking the computer to print data to the "Console" (the debug area at the bottom of Xcode).6. Variables and Constants (A High-Level Look)
A program needs to remember things (like a user's score, or their name). It remembers things using "boxes" in the computer's memory.-
If the box's contents *can change* later, we use
var(Variable).
-
If the box's contents *must never change*, we use
let(Constant).
*(We will cover this deeply in the next chapter, but here is a quick overview!)*
7. Basic Data Forms
Swift organizes data into different categories so it knows how to handle them.Strings (Text): Always wrapped in double quotes "".
Numbers (Integers & Decimals): Written as raw numbers. No quotes!
Booleans (True/False): The simplest form of logic. Used for on/off switches.
8. Full Swift Snippet: A Mini Text Game Logic
Let's put all the basics together into a single script.9. Common Mistakes
-
Putting Quotes Around Variables: If you type
print("currentHealth"), the computer will literally print the word "currentHealth". If you want the *value* stored inside the box (e.g., 80), you must drop the quotes:print(currentHealth).
-
Misusing Case Sensitivity: Swift is strictly case-sensitive.
var Score = 10andvar score = 10are two completely different variables.
10. Best Practices
-
camelCase Naming: Always name your variables starting with a lowercase letter, and capitalize every subsequent word. (e.g.,
userEmailAddress,highestPlayerScore).
-
Prefer
letovervar: Whenever possible, uselet. It makes your code faster and prevents accidental bugs where you change data that shouldn't be changed. Only usevarif you explicitly know the value will update.
11. Exercises
- 1. Write a single-line comment stating your name and today's date.
-
2.
Use the
print()statement to output the result of100 * 5.
12. Coding Challenges
Challenge: Create a constant namedappVersion and assign it the decimal number 1.5. Create a variable named isLoggedIn and assign it the boolean false. Finally, print both of them to the console.
13. MCQ Quiz with Answers
In Swift, how do you declare a piece of data in memory that is strictly forbidden from ever changing its value?
Which of the following correctly defines a Boolean value in Swift?
14. Interview Questions
-
Q: Explain the mechanical difference between a
varand aletin Swift. Why does Apple heavily encourage the default use ofletin iOS architecture?
- Q: Describe how Swift handles statement termination compared to legacy C-based languages. Are semicolons ever required in Swift?
-
Q: Contrast a String literal with a variable reference inside a
print()statement.
15. FAQs
Q: Where do I actually type this code in Xcode to test it? A: For pure Swift logic testing without building a full app, Xcode has a brilliant feature called a Playground. Go to File -> New -> Playground, select "Blank", and you can type Swift code and see the results instantly on the right side of the screen!16. Summary
In Chapter 3, we explored the foundational grammar of the Swift programming language. We discovered that Swift utilizes a clean, semicolon-free syntax. We practiced annotating our code with single and multi-line comments, and learned to output data via theprint() statement. Finally, we took a high-level look at memory allocation, distinguishing between mutable Variables (var) and immutable Constants (let), while identifying core data forms like Strings, Numbers, and Booleans.