How to support Swift Package Manager for existing projects

Issue #445

How to add SPM

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// swift-tools-version:5.1

import PackageDescription

let package = Package(
name: "Anchors",
platforms: [
.macOS(.v10_11),
.iOS(.v9),
.tvOS(.v9)
],
products: [
.library(
name: "Anchors",
targets: ["Anchors"]),
],
targets: [
.target(
name: "Anchors",
dependencies: [],
path: "Sources"
),
.testTarget(
name: "AnchorsTests",
dependencies: ["Anchors"]),
],
swiftLanguageVersions: [.v5]
)

To test, swift test to test locally, this should validate Package.swift too

Support multiple platform

To support multiple platform, use platform check

1
2
3
#if canImport(UIKit)
import UIKit
#endif

Use sources in Target

If this property is nil, all valid source files in the target’s path will be included and specified paths are relative to the target path.

A path can be a path to a directory or an individual source file. In case of a directory, the Swift Package Manager searches for valid source files recursively inside it.

1
2
3
4
5
6
.target(
name: "EasyClosureiOS",
dependencies: [],
path: "Sources",
sources: ["Shared", "iOS"]
)

Error

error: target ‘EasyClosuremacOS’ has sources overlapping sources

How to use

  • SPM packages are at DerivedData/MyApp/SourcePackages/checkouts
  • Delete DerivedData for Xcode to fetch new contents from GitHub
  • Clean build folder as Xcode often caches changes from frameworks

Read more

Comments