पाठ 2 / 25
Xcode, Playgrounds and the Swift Package Manager
Set up Swift tools and create a package from the command line.
Tools for writing Swift
On a Mac, Xcode is the full IDE for Apple-platform apps: editor, compiler, debugger, Interface Builder, simulators, Instruments for profiling and tools for signing and publishing. Swift Playgrounds (on Mac and iPad) and Xcode playgrounds let you experiment with code and see results immediately, which is great for learning. On Linux and Windows, install a toolchain from swift.org and use VS Code with the official Swift extension (built on SourceKit-LSP). The Swift Package Manager (SwiftPM) is built into the toolchain: swift package init creates a package with a Package.swift manifest, swift build compiles, swift run runs an executable target, and swift test runs tests. Dependencies are declared as Git URLs with version requirements in Package.swift, and Xcode understands packages too. Even if you mostly build apps in Xcode, putting reusable code into local Swift packages keeps modules small and testable.
Creating and running a Swift package
Package.swift declares targets and dependencies.
mkdir price-tools && cd price-tools
swift package init --type executable
swift build
swift run
swift test
# Package.swift (excerpt)
# // swift-tools-version: 6.0
# import PackageDescription
# let package = Package(
# name: "price-tools",
# dependencies: [
# .package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.0"),
# ],
# targets: [
# .executableTarget(name: "price-tools",
# dependencies: [.product(name: "ArgumentParser", package: "swift-argument-parser")]),
# .testTarget(name: "price-toolsTests", dependencies: ["price-tools"]),
# ]
# )A workshop and a parts catalogue
Xcode is the fully equipped workshop. SwiftPM is the parts catalogue and assembly line: it fetches the parts (dependencies) you list and builds the product the same way everywhere.
त्वरित जाँच: Which command runs the tests of a Swift package?
- swift run
- swift package init
- xcodebuild install
- swift test
Answer
swift test — swift test builds and runs the package's test targets.