Swift Programming Basics: A Complete Beginner’s Guide with Examples
March 3, 2026
If you want to build iOS apps for iPhone and iPad, learning Swift programming is your first step. Swift is modern, powerful, and beginner-friendly.
In this guide, we will cover:
- What is Swift
- Variables & Constants
- Data Types
- Conditionals (if/else, switch)
- Loops
- Functions
- Optionals (Very Important)
- Struct vs Class
- OOP Concepts in Swift
Let’s begin 🚀

What is Swift?
Swift is a powerful and intuitive programming language developed by Apple Inc. in 2014 for building apps for:
- iOS
- iPadOS
- macOS
- watchOS
- tvOS
Swift is:
- Safe (prevents common coding errors)
- Fast (high performance)
- Modern syntax
- Easy to read and write
It replaced Objective-C as the primary language for iOS development.
Variables & Constants in Swift
In Swift, we use:
var→ for variables (can change value)let→ for constants (cannot change value)
Example:
var name = "Saktidatt"
let country = "India"name = "John" // Allowed
// country = "USA" ❌ Error (cannot change constant)
💡 Best practice: Always use let unless the value needs to change.
Data Types in Swift
Swift is a strongly typed language.
Common Data Types:
| Type | Example |
|---|---|
| String | "Hello" |
| Int | 10 |
| Double | 10.5 |
| Float | 3.14 |
| Bool | true or false |
Example:
let age: Int = 30
let price: Double = 199.99
let isLoggedIn: Bool = true
Swift can also infer types automatically:
let city = "Bhubaneswar" // Swift understands this is String
Conditionals in Swift
Conditionals help you execute code based on conditions.
1️⃣ if / else
let age = 18if age >= 18 {
print("You are eligible to vote")
} else {
print("You are not eligible")
}
2️⃣ switch Statement
Swift’s switch is powerful and does NOT require break.
let grade = "A"switch grade {
case "A":
print("Excellent")
case "B":
print("Good")
default:
print("Keep Improving")
}
Loops in Swift
Loops help repeat code execution.
1️⃣ for-in Loop
for i in 1...5 {
print(i)
}
Output:
1 2 3 4 5
2️⃣ while Loop
var number = 1while number <= 5 {
print(number)
number += 1
}
Functions in Swift
Functions help organize reusable code.
Basic Function Example
func greet(name: String) {
print("Hello \(name)")
}greet(name: "Saktidatt")
Function with Return Value
func add(a: Int, b: Int) -> Int {
return a + b
}let result = add(a: 10, b: 20)
print(result)
Optionals in Swift (Very Important Topic ⚠️)
Optionals are one of the most important features in Swift.
An optional means:
👉 A variable may have a value OR it may be nil.
Declaring Optional
var middleName: String?
This means middleName can contain:
- A String
- OR nil
Why Optionals Matter?
Swift prevents null pointer exceptions using optionals.
1️⃣ Force Unwrapping (Not Recommended)
var name: String? = "John"
print(name!)
⚠️ If name is nil → app crashes.
2️⃣ Safe Unwrapping using if let (Recommended)
var name: String? = "John"if let safeName = name {
print(safeName)
}
3️⃣ Guard Let (Best Practice in Functions)
func printName(name: String?) {
guard let safeName = name else {
print("Name is nil")
return
}
print(safeName)
}
Struct vs Class in Swift
This is a very important interview topic.
Struct
struct Person {
var name: String
var age: Int
}
Class
class Employee {
var name: String
var salary: Double
init(name: String, salary: Double) {
self.name = name
self.salary = salary
}
}
Key Differences
| Struct | Class |
|---|---|
| Value Type | Reference Type |
| Stored in Stack | Stored in Heap |
| No Inheritance | Supports Inheritance |
| Faster | Slightly slower |
Example Difference
struct Car {
var name: String
}var car1 = Car(name: "BMW")
var car2 = car1
car2.name = "Audi"print(car1.name) // BMW (Original not changed)
With class, original object would change because it’s reference type.
OOP Concepts in Swift
Swift fully supports Object-Oriented Programming.
1️⃣ Encapsulation
class BankAccount {
private var balance: Double = 0
func deposit(amount: Double) {
balance += amount
}
func getBalance() -> Double {
return balance
}
}
2️⃣ Inheritance
class Animal {
func makeSound() {
print("Some sound")
}
}class Dog: Animal {
override func makeSound() {
print("Bark")
}
}
3️⃣ Polymorphism
let animal: Animal = Dog()
animal.makeSound() // Bark
4️⃣ Abstraction
Using protocols:
protocol Vehicle {
func start()
}class Bike: Vehicle {
func start() {
print("Bike started")
}
}
Final Thoughts
Swift is:
- Beginner friendly
- Powerful
- Safe
- Industry standard for iOS development
If you’re planning to become an iOS developer in 2026, mastering these Swift basics is mandatory before moving to:
- UIKit
- SwiftUI
- MVVM
- Clean Architecture
- Networking
- Concurrency