Swift Language Basics

June 02, 2026 1 min read

Swift is a modern, safe, expressive language. Here's the core you'll use constantly.

let vs var

let name = "Anand"   // constant
var score = 0         // variable
score += 10

Functions

func add(_ a: Int, _ b: Int) -> Int { a + b }
func greet(name: String = "friend") -> String { "Hello, \(name)" }

Structs vs classes

Structs are value types (copied) and preferred for models. Classes are reference types (shared) and used for things like view controllers.

struct User { let id: Int; var name: String }
var a = User(id: 1, name: "Asha")
var b = a        // a copy
b.name = "Bob"   // a.name stays "Asha"

Collections & closures

let nums = [1, 2, 3, 4]
let evens = nums.filter { $0 % 2 == 0 }   // [2, 4]
let doubled = nums.map { $0 * 2 }
Tip: Default to struct and let; reach for class/var only when you need reference semantics or mutation.

Summary

You can declare data, write functions, choose structs vs classes, and transform collections — the Swift foundation.