Swift Fundamentals Cheatsheet
Quick reference for Swift programming: syntax, data types, control flow, functions, OOP, protocols, and modern features.
| Feature | Description | Example | Category |
|---|---|---|---|
| Variables | Declare mutable variables | var name = "John" var age: Int = 25 | Basics |
| Constants | Declare immutable values | let pi = 3.14159 let maxUsers: Int = 100 | Basics |
| String Interpolation | Embed variables in strings | let greeting = "Hello, \(name)!" print(greeting) | Basics |
| Type Inference | Automatic type detection | let number = 42 // Inferred as Int let text = "Swift" // String | Basics |
| Arrays | Ordered collections | var numbers = [1, 2, 3, 4, 5] numbers.append(6) let first = numbers | Data Types |
| Dictionaries | Key-value pairs | var person = ["name": "John", "age": "25"] person["city"] = "NYC" | Data Types |
| Sets | Unique unordered values | var colors: Set = ["red", "blue", "green"] colors.insert("yellow") | Data Types |
| Tuples | Group multiple values | let http404 = (404, "Not Found") print(http404.0) // 404 | Data Types |
| If-Else | Conditional execution | if age >= 18 { print("Adult") } else { print("Minor") } | Control Flow |
| Switch | Multiple condition matching | switch grade { case "A": print("Excellent") case "B": print("Good") default: print("Other") } | Control Flow |
| For Loop | Iterate over sequences | for i in 1...5 { print(i) } for item in array { print(item) } | Control Flow |
| While Loop | Condition-based iteration | var count = 0 while count < 5 { count += 1 } | Control Flow |
| Guard | Early exit validation | guard let name = optionalName else { return } print(name) | Control Flow |
| Basic Function | Define reusable code blocks | func greet(name: String) -> String { return "Hello, \(name)!" } | Functions |
| Multiple Parameters | Functions with multiple inputs | func add(_ a: Int, _ b: Int) -> Int { return a + b } | Functions |
| Default Parameters | Optional parameter values | func greet(name: String = "Guest") { print("Hello, \(name)") } | Functions |
| Variadic Parameters | Accept variable number of arguments | func sum(_ numbers: Int...) -> Int { return numbers.reduce(0, +) } | Functions |
| Closures | Anonymous functions | let multiply = { (a: Int, b: Int) -> Int in return a * b } | Functions |
| Class | Define reference types | class Person { var name: String init(name: String) { self.name = name } } | OOP |
| Struct | Define value types | struct Point { var x: Int var y: Int } | OOP |
| Properties | Stored and computed properties | var fullName: String { return "\(firstName) \(lastName)" } | OOP |
| Methods | Functions within types | func speak() { print("Hello from \(name)") } | OOP |
| Inheritance | Extend existing classes | class Student: Person { var grade: String init(name: String, grade: String) { self.grade = grade super.init(name: name) } } | OOP |
| Protocol Definition | Define requirements | protocol Identifiable { var id: String { get } func identify() } | Protocols |
| Protocol Conformance | Implement protocol requirements | class User: Identifiable { var id: String func identify() { print(id) } } | Protocols |
| Protocol Extension | Add default implementations | extension Identifiable { func describe() { print("ID: \(id)") } } | Protocols |
| Optional Declaration | Values that may be nil | var middleName: String? = nil var age: Int? = 25 | Optionals |
| Optional Binding | Safely unwrap optionals | if let name = optionalName { print(name) } | Optionals |
| Force Unwrapping | Unwrap with ! (use carefully) | let forcedName = optionalName! print(forcedName) | Optionals |
| Nil Coalescing | Provide default values | let name = optionalName ?? "Guest" print(name) | Optionals |
| Optional Chaining | Safely access nested optionals | let length = person?.address?.street?.count | Optionals |
| Enums | Define enumeration types | enum Direction { case north, south, east, west } | Advanced |
| Generics | Write flexible, reusable code | func swap<T>(_ a: inout T, _ b: inout T) { let temp = a a = b b = temp } | Advanced |
| Error Handling | Handle errors with try-catch | do { try riskyOperation() } catch { print("Error: \(error)") } | Advanced |
| Extensions | Add functionality to existing types | extension String { func capitalized() -> String { return self.uppercased() } } | Advanced |
| Map/Filter/Reduce | Functional collection operations | let doubled = [1,2,3].map { $0 * 2 } let evens = [1,2,3].filter { $0 % 2 == 0 } | Advanced |