🐦

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.

Study this deck free

Try the first 10 cards

1 / 10
Question

Swift

Knew itLearning

Tap the card for the answer · swipe right if you knew it, left if not

Already have an account? Sign in

Cards

  1. 1Swift
    Modern programming language by Apple (2014) for iOS, macOS, watchOS, tvOS. Type-safe, fast, expressive. Successor to Objective-C. Open source
  2. 2Write a basic Hello World in Swift
    import Foundation print("Hello, World!") // No main function needed for scripts // Or in a function: func main() { print("Hello, World!") } main()
  3. 3Declare variables in Swift
    var 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)
  4. 4Swift's basic types
    Int, Int8/16/32/64, UInt Float, Double Bool String, Character Array<T>, Dictionary<K,V>, Set<T> Optional<T>
  5. 5Optional in Swift
    Type 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
  6. 6Optional binding
    Safe way to unwrap optionals: if let name = optionalName { print(name) // unwrapped } guard let name = optionalName else { return } // name available here
  7. 7Optional chaining
    person?.address?.city Calls method/property only if not nil Returns optional result Stops if any part is nil let count = name?.count ?? 0
  8. 8Nil coalescing operator
    let name = optionalName ?? "default" Provides default value if optional is nil Equivalent to: optionalName != nil ? optionalName! : "default"
  9. 9Forced unwrapping
    let name = optionalName! Asserts optional has value, crashes if nil Use only when 100% certain it's not nil Prefer optional binding or guard
  10. 10Implicitly unwrapped optional
    var 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
  11. 11Define a function in Swift
    func 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) }
  12. 12Argument labels in Swift
    func 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")
  13. 13Closures in Swift
    Anonymous 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 }
  14. 14Struct in Swift
    struct Person { var name: String var age: Int func greet() { print("Hello, \(name)!") } } let person = Person(name: "John", age: 30) Value type, copied on assignment
  15. 15Class in Swift
    class 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
  16. 16struct vs class
    Struct: 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
  17. 17Protocol in Swift
    protocol Drawable { var color: String { get set } func draw() } struct Circle: Drawable { var color: String func draw() { print("Drawing circle") } } Like interface, defines contract
  18. 18Protocol extension
    extension Drawable { func describe() { print("Color: \(color)") } } Provides default implementation All conforming types get method No need to implement in each type
  19. 19Enum in Swift
    enum Direction { case north, south, east, west } // With associated values: enum Result { case success(String) case failure(Error) } let result = Result.success("OK")
  20. 20Use switch with enums
    switch 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
  21. 21Pattern matching with associated values
    switch result { case .success(let message): print("Success: \(message)") case .failure(let error): print("Error: \(error)") } if case .success(let msg) = result { print(msg) }
  22. 22Guard statement
    guard let name = optionalName else { return } // name available here Early exit pattern Must transfer control out (return, throw, break, continue) Unwrapped value available after guard
  23. 23Defer statement
    func processFile() { let file = openFile() defer { closeFile(file) // always runs } // process file return // defer executes here } Executes when leaving scope Useful for cleanup
  24. 24Type casting in Swift
    is: 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
  25. 25Any vs AnyObject
    Any: 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
  26. 26Property observer
    var score: Int = 0 { willSet(newValue) { print("About to set to \(newValue)") } didSet(oldValue) { print("Changed from \(oldValue) to \(score)") } } Called when property changes
  27. 27Computed properties
    struct 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
  28. 28Lazy property
    class DataManager { lazy var database = Database() } Initialized only when first accessed Must be var (not let) Not thread-safe Useful for expensive initialization
  29. 29Static vs class in Swift
    static: cannot be overridden static func myFunc() { } class: can be overridden in subclasses class func myFunc() { } Both are type-level (not instance-level)
  30. 30Inheritance in Swift
    class 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
Study all 48 cards free