Swift Essentials
48 cards · 🇺🇸 English
Core Swift syntax, optionals, and iOS-friendly language features.
Study all 48 cards for free
Flashcards, quizzes, pixel-art games and a daily review that remembers for you.
Try the first 10 cards
1 / 10Swift
Tap the card for the answer · swipe right if you knew it, left if not
Already have an account? Sign in
Cards
- 1SwiftModern programming language by Apple (2014) for iOS, macOS, watchOS, tvOS. Type-safe, fast, expressive. Successor to Objective-C. Open source
- 2Write a basic Hello World in Swiftimport Foundation print("Hello, World!") // No main function needed for scripts // Or in a function: func main() { print("Hello, World!") } main()
- 3Declare variables in Swiftvar name = "John" // mutable, inferred type var age: Int = 25 // explicit type let pi = 3.14 // immutable constant let city: String = "Paris" Prefer let over var (immutability)
- 4Swift's basic typesInt, Int8/16/32/64, UInt Float, Double Bool String, Character Array<T>, Dictionary<K,V>, Set<T> Optional<T>
- 5Optional in SwiftType that can hold a value or nil: var name: String? = "John" var age: Int? = nil Must unwrap before use: if let unwrapped = name { print(unwrapped) } Swift's null safety
- 6Optional bindingSafe way to unwrap optionals: if let name = optionalName { print(name) // unwrapped } guard let name = optionalName else { return } // name available here
- 7Optional chainingperson?.address?.city Calls method/property only if not nil Returns optional result Stops if any part is nil let count = name?.count ?? 0
- 8Nil coalescing operatorlet name = optionalName ?? "default" Provides default value if optional is nil Equivalent to: optionalName != nil ? optionalName! : "default"
- 9Forced unwrappinglet name = optionalName! Asserts optional has value, crashes if nil Use only when 100% certain it's not nil Prefer optional binding or guard
- 10Implicitly unwrapped optionalvar name: String! = "John" Optional that doesn't need unwrapping Still crashes if nil Use for: IBOutlets, properties initialized after init let greeting = "Hello, " + name // no unwrapping
- 11Define a function in Swiftfunc greet(name: String) -> String { return "Hello, \(name)!" } // Multiple parameters: func add(a: Int, b: Int) -> Int { return a + b } // No return: func printName(_ name: String) { print(name) }
- 12Argument labels in Swiftfunc greet(person name: String, from hometown: String) { print("Hello \(name) from \(hometown)!") } greet(person: "John", from: "Paris") // Omit label: func greet(_ name: String) { } greet("John")
- 13Closures in SwiftAnonymous functions: let add = { (a: Int, b: Int) -> Int in return a + b } // Short syntax: let nums = [1, 2, 3, 4] let doubled = nums.map { $0 * 2 } // Trailing closure: UIView.animate(withDuration: 1.0) { view.alpha = 0 }
- 14Struct in Swiftstruct Person { var name: String var age: Int func greet() { print("Hello, \(name)!") } } let person = Person(name: "John", age: 30) Value type, copied on assignment
- 15Class in Swiftclass Person { var name: String var age: Int init(name: String, age: Int) { self.name = name self.age = age } deinit { // cleanup } } Reference type, shared on assignment
- 16struct vs classStruct: value type (copied), no inheritance, immutable by default, no deinit Class: reference type (shared), inheritance, can mutate in let, has deinit Prefer struct unless need: - Inheritance - Reference semantics - Deinitializer
- 17Protocol in Swiftprotocol Drawable { var color: String { get set } func draw() } struct Circle: Drawable { var color: String func draw() { print("Drawing circle") } } Like interface, defines contract
- 18Protocol extensionextension Drawable { func describe() { print("Color: \(color)") } } Provides default implementation All conforming types get method No need to implement in each type
- 19Enum in Swiftenum Direction { case north, south, east, west } // With associated values: enum Result { case success(String) case failure(Error) } let result = Result.success("OK")
- 20Use switch with enumsswitch direction { case .north: print("Going north") case .south: print("Going south") case .east, .west: print("Going east or west") } Must be exhaustive (or use default:) No fallthrough by default
- 21Pattern matching with associated valuesswitch result { case .success(let message): print("Success: \(message)") case .failure(let error): print("Error: \(error)") } if case .success(let msg) = result { print(msg) }
- 22Guard statementguard let name = optionalName else { return } // name available here Early exit pattern Must transfer control out (return, throw, break, continue) Unwrapped value available after guard
- 23Defer statementfunc processFile() { let file = openFile() defer { closeFile(file) // always runs } // process file return // defer executes here } Executes when leaving scope Useful for cleanup
- 24Type casting in Swiftis: type checking if item is Movie { } as?: conditional cast (returns optional) if let movie = item as? Movie { } as!: forced cast (crashes if fails) let movie = item as! Movie
- 25Any vs AnyObjectAny: any type (class, struct, enum, function) var things: [Any] = [1, "hello", true] AnyObject: any class type (reference types only) var objects: [AnyObject] = [Person(), Car()] Prefer specific types over Any
- 26Property observervar score: Int = 0 { willSet(newValue) { print("About to set to \(newValue)") } didSet(oldValue) { print("Changed from \(oldValue) to \(score)") } } Called when property changes
- 27Computed propertiesstruct Rectangle { var width: Double var height: Double var area: Double { get { return width * height } set(newArea) { width = sqrt(newArea) height = sqrt(newArea) } } } No storage, calculated on access
- 28Lazy propertyclass DataManager { lazy var database = Database() } Initialized only when first accessed Must be var (not let) Not thread-safe Useful for expensive initialization
- 29Static vs class in Swiftstatic: cannot be overridden static func myFunc() { } class: can be overridden in subclasses class func myFunc() { } Both are type-level (not instance-level)
- 30Inheritance in Swiftclass Vehicle { var speed = 0 func describe() { } } class Car: Vehicle { var gear = 1 override func describe() { super.describe() print("Car at \(speed) km/h") } } final class prevents inheritance
And 18 more cards. Study the whole deck free
Articles about this deck
More Programming decks
See all- 🐹
Go Essentials
49 cards · 🇺🇸 English
Core Go syntax: goroutines, channels, slices, and error handling.
- 🎯
Kotlin Essentials
42 cards · 🇺🇸 English
Core Kotlin syntax, null safety, and Android-friendly language features.
- 🦀
Rust Essentials
48 cards · 🇺🇸 English
Ownership, borrowing, and core Rust syntax.
- 🧱
Data Structures
95 cards · 🇺🇸 English
Arrays, linked lists, trees, graphs, hash tables and their core operations.
- 🟣
C#: Fundamentals & .NET Core
99 cards · 🇺🇸 English
Core C# syntax, types, and .NET Core concepts in one deck.
- 🌐
APIs: REST, GraphQL & HTTP Status Codes
108 cards · 🇺🇸 English
REST and GraphQL API concepts plus common HTTP status codes.
