Chapter 2: Swift Get Started

Swift – Get Started (Very Detailed Beginner Lesson)

0. Where should I write and run Swift code today?

You have these realistic choices right now (2025–2026):

Method Needs Mac? First line in… Best for right now? Recommendation
Swift Playgrounds (app) Yes ~15 seconds ★★★★★ Best starting point
Online playground No ~10 seconds ★★★★ Very good backup
Xcode Yes 2–5 minutes ★★★ After 1–2 weeks
VS Code + Swift extension Any 15–40 min setup ★★ Later (scripting)

Strong recommendation for the next 1–2 weeks:

→ Open Swift Playgrounds on Mac or iPad OR go to https://swiftfiddle.com (online, zero installation)

Both let you write and run code instantly.

Let’s begin!

1. Your very first line

Create a new blank playground and write:

Swift

Run it (press the play button ▶️ or Cmd+R in Xcode/Playgrounds).

You should see:

text

That’s it — you just executed Swift code. 🎉

2. Comments — explaining your code to future you

Swift

Good habit: write small comments explaining why you did something — not just what.

3. Naming things — variables & constants

Think of variables as named boxes.

Swift

Very important rule most beginners break at first:

Use let by default — only use var when you are sure you will change the value.

Common beginner mistake:

Swift

Better:

Swift

4. Most common types you’ll meet every day

Type Examples Real-life meaning Default choice?
Int 0, 42, -17, 1000000 Age, count, ID, quantity Yes
Double 3.14, 1.78, -0.05, 99.99 Height, price, temperature, percentage Yes
Bool true, false Is raining? Is logged in? Is adult? Yes
String “Hello”, “কলকাতা”, “🚀”, “” Name, message, address, label Yes

Examples:

Swift

You usually don’t need to write the type — Swift guesses it very well.

But you can write it when you want to be super clear:

Swift

5. Strings — you will use them constantly

Swift

Useful things you do 100 times a day:

Swift

6. Doing decisions — if statement

Swift

Tip: Write the most common situation first — code becomes easier to read.

7. Doing many decisions — switch

Swift

Very common modern pattern:

Swift

8. Lists of things — Arrays

Swift

Most common loop:

Swift

With numbering (very common):

Swift

9. The most important concept in Swift: Optionals

Real-life analogy: You ask someone: “Do you have a nickname?”

  • Sometimes they say “Yes — Sunny”
  • Sometimes they say “No” (nothing / nil)

In Swift:

Swift

Danger — you cannot just use it:

Swift

Safe ways (these are the correct ones):

Swift

Quick Recap Table – What You Already Know

Concept Main idea Most common way to write it
Constant Never changes let name = “Amit”
Variable Can change var count = 0
String Text “Hello \(name)”
Decision Choose different path if temperature > 30 { … }
Multiple choices Many clean cases switch status { case 200..<300: … }
List Ordered collection var items = [“apple”, “banana”]
Maybe-value Can be value or nothing var email: String? + if let email { … }

Your First Mini-Exercise (try these right now)

Copy-paste each one into a playground and run:

  1. Print your name and city using string interpolation
Swift
  1. Check temperature and print message
Swift
  1. Make a list of 4 favorite foods and print them numbered
Swift
  1. Create an optional variable and print it safely
Swift

Next logical step — which one feels most interesting to you?

  1. Optionals in more depth (very important)
  2. Functions — make reusable code blocks
  3. Structs — create your own data types (like a Person, Book, Order…)
  4. SwiftUI — make your first real button & text on screen
  5. Loops + arrays more deeply

Just tell me which direction you want to go next — I’ll continue in the same detailed, teacher-like style 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *