Browse Questions
  • The value you assign replaces the one that was just set, and the observers are not triggered again, so there is no infinite loop.
  • SPM is Apple's built-in dependency manager for Swift.
  • The Observer pattern lets objects subscribe to events from another object without tight coupling.
  • @AppStorage is a property wrapper that reads and writes to UserDefaults and automatically refreshes the view when the value changes.

Answer: SPM is Apple's built-in dependency manager for Swift. It fetches, builds, and links third-party packages directly inside Xcode — no extra tools like CocoaPods or Carthage needed.

How to add a package in Xcode:

  1. File → Add Package Dependencies
  2. Paste the repo URL (e.g. https://github.com/Alamofire/Alamofire)
  3. Choose version rule → Add Package

Creating your own package:

// Package.swift
let package = Package(
    name: "NetworkKit",
    platforms: [.iOS(.v16)],
    products: [
        .library(name: "NetworkKit", targets: ["NetworkKit"])
    ],
    dependencies: [
        .package(url: "https://github.com/Alamofire/Alamofire", from: "5.0.0")
    ],
    targets: [
        .target(
            name: "NetworkKit",
            dependencies: ["Alamofire"]
        ),
        .testTarget(
            name: "NetworkKitTests",
            dependencies: ["NetworkKit"]
        )
    ]
)

Version rules:

  • .exact("5.2.0") — pin to a specific version
  • from: "5.0.0" — any version from 5.0.0 up to the next major (< 6.0.0)
  • .upToNextMinor(from: "5.2.0") — < 5.3.0