skip to content

App Integration and Binaries

Apps consume packages through Xcode, split features into local packages and ship closed SDKs as binary targets. Interviewers ask how modularizing with packages speeds builds and tests.

on this pageshow

explore

questions

5

When you add a Swift package to an iOS app in Xcode, what do you choose, and why can an import still fail?

level: juniorimportance: must knowfreq 70%

answer

  1. two separate steps
  2. a URL plus a version requirement
  3. products are linked per target
  4. import the module, not the package

basics

~20 s

You give Xcode the package's repository URL and a version requirement, then choose which library products to link into which targets. Declaring the package links nothing: importing a module that no linked product brings in fails with No such module.

solid answer

~50 s

Adding a package is two steps. First the project gains a **package dependency**: a repository URL plus a version requirement, typically "from `2.3.0` up to the next major version"; the resolved versions are recorded in `Package.resolved`. Second, you choose which of the package's **library products** to link into which target — the app, a widget extension, a test bundle. A product is what another project depends on; the targets inside it are Swift modules, and your code imports a module name and sees only its `public` API. So `import Payments` fails with `No such module` when nothing the target links brings in `Payments`, and a package whose `platforms` require a newer OS than the target fails the build. Plain SwiftPM has the same split: a dependency no target uses only earns a "not used by any target" warning.

code

bash · 2 lines
bash
swift package add-dependency https://github.com/apple/example-package-playingcard --from 3.0.0
swift package add-target-dependency PlayingCard DeckOfPlayingCards --package example-package-playingcard

go deeper

for a junior

Recall the two steps: declare the package with a URL and a version requirement, then link a library product to each target that imports it. Know that you import a module name.

for a middle

Explain products versus targets versus modules, why an unlinked product gives No such module, and how a package's platforms list can fail an app's build.

for a senior

Diagnose a failing import quickly: wrong target linked, product versus module name, deployment target below the package's platforms, or a resolution failure upstream of linking.

for a principal

Set the team rule for adding a dependency: what to read in its manifest first, who approves new products in the app, and why each link should be deliberate rather than blanket.

## Two things happen when you add a package When an iOS project takes a third-party Swift package, two separate decisions are recorded, and most "it won't import" problems come from confusing them. 1. **The package dependency.** The project learns *where* the package lives (a Git repository URL) and *which versions* are acceptable (a version requirement). Swift Package Manager (SwiftPM) resolves the whole dependency graph — the package and everything it depends on — and records the exact versions it chose in a file named `Package.resolved`. 2. **Linking products to targets.** A package declares **products**: libraries, executables and plugins. An app target can only link a **library product**. You choose, per target, which products to link: the app target, a widget or share extension, a test bundle. Each target that imports the code needs its own link. Declaring the dependency alone makes nothing importable. SwiftPM's own documentation shows the same rule from the command line: after adding a dependency to a package manifest, `swift build` succeeds with a warning that the dependency "is not used by any target", and `import PlayingCard` fails with `No such module 'PlayingCard'` until a target depends on the product. ## Products, targets and modules | Term | What it is | Who refers to it | |---|---|---| | **Package dependency** | A repository URL plus a version requirement | The project or a `Package.swift` manifest | | **Product** | A named library (or executable, or plugin) a package vends | The target that links it | | **Target** | A building block inside the package; each one is a module | Other targets in the same package | | **Module** | The unit of code and access control you `import` | Your Swift source files | The names can differ. A product called `Networking` may bundle two targets, `HTTPCore` and `HTTPCache`; your source writes `import HTTPCore`, the module name, not `import Networking`. Only the module's `public` (and `open`) declarations are visible to the app — the language rules for access levels are covered with Swift's access control. ## Why an import still fails - **The product is linked to a different target.** It was added to the app, but the widget extension that imports the same module was never given the product. - **The import names the product or repository, not the module.** The package `example-package-playingcard` vends a library whose module is `PlayingCard`. - **The package's `platforms` exceed the target's deployment target.** If the manifest declares `.iOS(.v17)` and the app target supports iOS 16, the build fails with an error that the package product requires a newer minimum platform version than the target supports. - **The API is not public.** The import succeeds, but using an `internal` type reports that it is inaccessible due to its protection level — a different error from `No such module`. - **Resolution failed.** If SwiftPM cannot find versions that satisfy every requirement in the graph, nothing from the package builds; that is a version-requirement problem, not a linking one. ## The same steps in a pure package Outside Xcode, the two steps are two manifest edits, and SwiftPM ships commands that make them: - `swift package add-dependency <url> --from <version>` adds the `.package(url:from:)` entry. - `swift package add-target-dependency <product> <target> --package <package>` adds the product to a target's `dependencies`. Seeing both edits side by side is the clearest way to explain the Xcode flow in an interview: the dependency says *what may be fetched*, the target dependency says *who links it*. ## Finding a package before you add it The **Swift Package Index** is a public index of Swift packages that also hosts package documentation; SwiftPM's own getting-started guide links the swift-testing documentation there. Before adding a package, read its `Package.swift`: the `platforms` it requires, the products it vends, whether it pulls in a closed-source binary target, and whether its release tags are full three-part semantic versions — SwiftPM interprets only tags with major, minor and patch components as versions. ## What a reviewer looks for - A product linked only into the targets that import it, not into every target "just in case". - A version requirement that allows bug-fix releases rather than a hard-coded commit. - `Package.resolved` committed for the app, so every machine builds the same versions. - Import lines that name modules, and no reliance on a module that merely happens to be reachable through another product.

  • The app target links a package's product, but the app's widget extension imports the same module and fails to build. Why?
    Linking is per target. The product was linked into the app target only, so the extension's compile has no such module. Link the product into the extension target as well; the package dependency itself is declared once for the project and does not need to be added again.
  • A package vends a product named `Networking` made of the targets `HTTPCore` and `HTTPCache`. What does app code import?
    The module it uses: `import HTTPCore` and, if needed, `import HTTPCache`. A library product is a bundle of targets, and each target is its own module. The product name is what you link; the module name is what you import.
  • What do you check in a package before adding it to a production app?
    Its `Package.swift`: the `platforms` it requires against your deployment target, the library products it vends, any binary targets it downloads, and its tools version. Then its release tags, which must be full major.minor.patch versions for SwiftPM to treat them as versions, its license and its documentation, which the Swift Package Index hosts for many packages.

  • Adding the package URL to the project makes its modules importable in every target.
  • You import the repository or product name, so the import must match the package's URL.
  • A No such module error means the API you use is marked internal.
  • A package whose platforms require iOS 17 still builds into an app that targets iOS 16.
  • Every product a package vends gets linked into the app automatically.
open as a page

In a Swift package target that bundles resources, how does its code load them through Bundle.module, and why does Bundle.main miss them?

level: middleimportance: should knowfreq 48%

basics

~20 s

SwiftPM puts a target's resources in their own bundle and generates an internal Bundle.module accessor for that module. Package code loads files and assets through Bundle.module; Bundle.main is the app's bundle, which does not hold them.

open as a page

How does an iOS app consume a local Swift package kept in its own repository, and how does that differ from a remote package dependency?

level: middleimportance: should knowfreq 45%

basics

~20 s

A local package is a folder with its own Package.swift inside the app's repository; the app links its library products like any package. SwiftPM uses whatever code sits at the path: no version requirement, no tag, edits build immediately.

open as a page

A vendor ships a closed-source analytics SDK as a zipped XCFramework; how do you declare it with SwiftPM's binaryTarget, and what does its checksum guard?

level: seniorimportance: should knowfreq 30%

basics

~10 s

Declare .binaryTarget(name:url:checksum:) with an https URL to the zip and the SHA-256 that swift package compute-checksum prints. SwiftPM rejects a downloaded archive whose checksum differs, so a swapped or corrupted file never builds.

open as a page

As the lead moving a 300k-line iOS app's features into local Swift packages, where do you draw the package and target seams, and what does it cost?

level: principalimportance: should knowfreq 38%

basics

~20 s

Start with one local package: shared leaf targets (design system, networking, vendor wrappers) and one target and library product per feature, depending only downward. The cost is public API, Bundle.module rewrites, manifest upkeep and many more modules to build.

open as a page