Swift has modular code, which can be sorted into modules.
But you can use the Swift Package Manager to fetch and install modules.
Is there any difference between a "Swift package" and a "Swift module"? When should I use one phrase or the other?
Swift packages are reusable components of Swift, Objective-C, Objective-C++, C, or C++ code that developers can use in their projects. They bundle source files, binaries, and resources in a way that's easy to use in your app's project.
Packages that vend a library product help promote modularity in your code, make it easy to share code with others, and enable other developers to add functionality to their apps. With Xcode, you can create a new Swift package, add code, resource files, and binaries, build the Swift package, and run its unit tests.
A library's product can be either statically or dynamically linked. It's recommended that you don't explicity declare the type of library, so Swift Package Manager can choose between static or dynamic linking based on the preference of the package's consumer.
Open your Xcode project, navigate the File tab within the macOS bar, and click on “Add Packages”. In the Add New Package window, you can select from recently used or Apple Swift Packages. Alternatively, you can search for a package via the name or the URL to the Github page.
As mentioned in Swift.org
Modules: Swift organizes code into modules. Each module specifies a namespace and enforces access controls on which parts of that code can be used outside of the module.
A program may have all of its code in a single module, or it may import other modules as dependencies. Aside from the handful of system-provided modules, such as Darwin on macOS or Glibc on Linux, most dependencies require code to be downloaded and built in order to be used.
When you use a separate module for code that solves a particular problem, that code can be reused in other situations. For example, a module that provides functionality for making network requests can be shared between a photo sharing app and a weather app. Using modules lets you build on top of other developers’ code rather than reimplementing the same functionality yourself.
Packages: A package consists of Swift source files and a manifest file. The manifest file, called Package.swift, defines the package’s name and its contents using the PackageDescription module.
A package has one or more targets. Each target specifies a product and may declare one or more dependencies.
A Swift package is a collection of source code... you can think about it as a self contained project, which has its own versions and repository. The Swift language itself doesn't know about packages currently, it is concept only used by the Swift package manager.
A Swift package can contain one or more Swift modules -- each Swift module is something the language knows about, and the Swift modules defines the scope of things like access control (public
, internal
, private
) for example. A Swift package can also contain other kinds of targets or modules, for example it can contain a body of C source code to build into a library.
Swift packages are in some sense analogous to Xcode projects, and Swift modules are analogous to Xcode targets.
As a concrete example https://github.com/apple/example-package-fisheryates
is a Swift package (as signified by the Package.swift
at the top-level), identified by its URL, and it contains a single Swift module with two sources:
https://github.com/apple/example-package-fisheryates/tree/master/Sources.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With