Create and manage Swift Packages for iOS development, including local packages, dependencies, and package plugins. Use when creating Swift packages, managing dependencies, configuring Package.swift, adding resources, or troubleshooting SPM issues. Triggers on Swift Package, SPM, Package.swift, local package, dependency, package plugin, package resolution.
You are an expert in Swift Package Manager. When this skill activates, help create and manage Swift packages effectively.
# Create new package
mkdir MyPackage && cd MyPackage
swift package init --type library --name MyPackage
# Create executable
swift package init --type executable
# Create with specific tools version
swift package init --type library
// swift-tools-version: 5.9
import PackageDescription
let package = Package(
// MARK: - Package Identity
name: "DesignSystem",
// MARK: - Default Localization
defaultLocalization: "en",
// MARK: - Supported Platforms
platforms: [
.iOS(.v17),
.macOS(.v14),
.watchOS(.v10),
.tvOS(.v17),
.visionOS(.v1)
],
// MARK: - Products (What others can import)
products: [
// Main library
.library(
name: "DesignSystem",
targets: ["DesignSystem"]
),
// Dynamic library (for faster debug builds)
.library(
name: "DesignSystemDynamic",
type: .dynamic,
targets: ["DesignSystem"]
),
// Plugin
.plugin(
name: "SwiftLintPlugin",
targets: ["SwiftLintPlugin"]
)
],
// MARK: - Dependencies
dependencies: [
// Remote package (exact version)
.package(url: "https://github.com/apple/swift-algorithms", exact: "1.2.0"),
// Remote package (version range)
.package(url: "https://github.com/pointfreeco/swift-composable-architecture", from: "1.5.0"),
// Remote package (branch)
.package(url: "https://github.com/user/repo", branch: "main"),
// Remote package (commit)
.package(url: "https://github.com/user/repo", revision: "abc123"),
// Local package
.package(path: "../Core"),
.package(path: "../Networking"),
// Conditional (for development)
// .package(url: "...", from: "1.0.0"),
],
// MARK: - Targets
targets: [
// Main target
.target(
name: "DesignSystem",
dependencies: [
// Product from package
.product(name: "Algorithms", package: "swift-algorithms"),
// Local target
"DesignSystemCore",
// Conditional dependency
.target(name: "DesignSystemMacOS", condition: .when(platforms: [.macOS]))
],
path: "Sources/DesignSystem",
exclude: ["README.md"],
resources: [
// Process resources (localization, asset catalogs)
.process("Resources"),
// Copy as-is
.copy("Fonts")
],
swiftSettings: [
.define("DEBUG", .when(configuration: .debug)),
.enableUpcomingFeature("StrictConcurrency"),
.enableExperimentalFeature("VariadicGenerics")
],
linkerSettings: [
.linkedFramework("CoreGraphics"),
.linkedLibrary("sqlite3")
]
),
// Core target (no dependencies)
.target(
name: "DesignSystemCore"
),
// Platform-specific target
.target(
name: "DesignSystemMacOS",
dependencies: ["DesignSystemCore"]
),
// Test target
.testTarget(
name: "DesignSystemTests",
dependencies: [
"DesignSystem",
.product(name: "CustomDump", package: "swift-custom-dump")
],
resources: [
.process("Fixtures")
]
),
// Plugin target
.plugin(
name: "SwiftLintPlugin",
capability: .buildTool(),
dependencies: [
.target(name: "SwiftLintBinary")
]
),
// Binary target
.binaryTarget(
name: "SwiftLintBinary",
url: "https://github.com/.../swiftlint.zip",
checksum: "abc123..."
)
],
// MARK: - Swift Language Version
swiftLanguageVersions: [.v5]
)
let package = Package(
name: "Networking",
platforms: [.iOS(.v17)],
products: [
.library(name: "Networking", targets: ["Networking"]),
.library(name: "NetworkingMocks", targets: ["NetworkingMocks"])
],
targets: [
.target(name: "Networking"),
.target(
name: "NetworkingMocks",
dependencies: ["Networking"]
),
.testTarget(
name: "NetworkingTests",
dependencies: ["Networking", "NetworkingMocks"]
)
]
)
let package = Package(
name: "Analytics",
platforms: [.iOS(.v17)],
products: [
.library(name: "AnalyticsInterface", targets: ["AnalyticsInterface"]),
.library(name: "Analytics", targets: ["Analytics"]),
.library(name: "AnalyticsMock", targets: ["AnalyticsMock"])
],
targets: [
// Protocol only - no implementation
.target(name: "AnalyticsInterface"),
// Real implementation
.target(
name: "Analytics",
dependencies: ["AnalyticsInterface"]
),
// Mock for testing
.target(
name: "AnalyticsMock",
dependencies: ["AnalyticsInterface"]
),
.testTarget(
name: "AnalyticsTests",
dependencies: ["Analytics", "AnalyticsMock"]
)
]
)
.target(
name: "DesignSystem",
resources: [
// Asset catalogs, localized strings, storyboards
.process("Resources"),
// Copy files exactly as-is
.copy("Fonts/CustomFont.ttf"),
// Specific file
.process("config.json")
]
)
// Bundle for this module
let bundle = Bundle.module
// Load image
let image = UIImage(named: "icon", in: .module, with: nil)
// SwiftUI image
Image("icon", bundle: .module)
// Load data file
if let url = Bundle.module.url(forResource: "config", withExtension: "json") {
let data = try Data(contentsOf: url)
}
// Localized strings
Text("welcome_message", bundle: .module)
Sources/
└── DesignSystem/
└── Resources/
├── Assets.xcassets/
│ ├── Colors/
│ │ └── Primary.colorset/
│ └── Images/
│ └── Logo.imageset/
└── Localizable.xcstrings