Swift packages and modules
Swift packages and modules
A growing report tool needs more than one source file. A module is a unit of code imported by name. Swift Package Manager organizes targets, dependencies and build settings in a package manifest, allowing reusable library and executable components.
What you’ll learn
- Identify a package manifest, targets and imported modules.
- Choose local build and test commands for a multi-file project.
Swift packages and modules in the example
Manifest
A growing report tool needs more than one source file.
ManifestTargets and modules
Create an executable package in a new empty directory with <code>swift package init --type executable</code>.
Targets and modulesBuilt executable
This single-file example imports Foundation and uses a standard collection operation to form a report label.
Built executable
How swift packages and modules works
Create an executable package in a new empty directory with swift package init --type executable. Inspect Package.swift and the generated source layout, then use swift run. The generated paths can depend on the toolchain template, so inspect them instead of blindly overwriting a presumed file.
Work through swift packages and modules
Save this complete program as main.swift and run it with the Swift toolchain. No separate input file is needed.
Swift · complete program
import Foundation
let labels = ["Diya", "Saira"]
print(labels.joined(separator: ", "))Expected output
Diya, SairaFollow the execution
This single-file example imports Foundation and uses a standard collection operation to form a report label. For a package exercise, place the relevant statements in the generated executable source and run the package. Importing a module differs from copying all its source into this file.
Keep library and executable targets separate
A library target can own calculation logic while an executable target owns input and display. Tests can then exercise the calculation without parsing console text. Expose only the API callers need, and keep file paths and environment-specific choices outside reusable arithmetic.
Practice: Swift packages and modules
Your task: What is Package.swift responsible for?
Swift · complete program
import Foundation
let labels = ["Diya", "Saira"]
print(labels.joined(separator: ", "))Worked answer
It declares package configuration such as products, targets and dependencies. It is not a student dataset or the executable’s ordinary report output.
Separate configuration from program data
Use swift build to build and swift test for configured tests. Third-party dependencies introduce version, licence and trust decisions. Importing a name alone does not download or configure an arbitrary package in the single-file online compiler.
Check your decision
Separate configuration from program data
It declares package configuration such as products, targets and dependencies. It is not a student dataset or the executable’s ordinary report output. Use swift build to build and swift test for configured tests.
Further reading: official Swift language guide.
Reaching the recap and completing the quizzes are separate milestones.