Use swift package manager on existing xcode project
Swift Package Manager is a standalone tool which allows managing dependencies and building projects without Xcode. It can generate Xcode projects for you with swift package generate-xcodeproj
.
However, at the moment, Swift Package Manager only has support for building projects for macOS and Linux platforms. The only way to build projects for iOS, tvOS and watchOS is using Xcode, which includes the SDKs needed for these platforms.
There are ways to use Swift Packages Manager to manage dependencies for iOS/tvOS/watchOS, but it is not easy and requires manual work. If you are interested, take a look at https://github.com/j-channings/swift-package-manager-ios
Other than that, I'd recommend using Carthage or CocoaPods.
Update for Xcode 11
Swift Package Manager is now integrated into Xcode 11. You can add your package by going to "File" then "Swift Packages" then "Add Package Dependency..." Paste the repository's URL into the field above then click "next". Xcode will walk you through the rest of the steps. You can learn more at this WWDC talk.
Exporting Package.swift for Swift Package Manager from existing Xcode project
There are no a menu command or utility to convert application to a static library, dynamic framework or swift package since they are different types of projects with different settings etc.
If you want to export a part of your project as a swift package you should make next steps manually:
1. Create Package.swift file in the root of your project
import PackageDescription
let package = Package(
name: “MyLib”,
products: [
.library(name: "MyLib", targets: ["MyLib"])
],
targets: [
.target(name: "MyLib"),
],
...
)
2. Make folder with subfolder ./Sources/MyLib
under the projects’s root.
By default swift package structure requires to put all your sources files under Sources/LibraryName
folder but you can change it below.
NOTE: you can simplify first two steps by using swift package init
and it creates Package.swift, Sources and Test folders etc.
3. Include source files
a) Move the needed files to share from their current locations to MyLib folder.
For instance:
./Classes/MyEntity.swift -> ./Sources/MyLib/MyEntity.swift
Also you have to update locations of the moved files in your Xcode project to leave it compilable.
b) Use path
, sources
and exclude
to point needed source files to your package from their current locations:
.target(name: "MyLib", path: "Classes"),
NOTE: Don't forget to make your classes public to access to them after import your package:
public class MyEntity {
...
}
After all you will have two working projects - old XCode's one and new Swift package.
4. REPL
Now you can use command line interpreter with your swift package:
swift run --repl
import MyLib
let entity = MyEntity()
...
Swift package manager for project migrated from Swift 2
Repositories under Swift Package Manager have certain layout. At the root of the repo you have Package.swift
that provides a manifest for the repository, its name, type, dependencies, etc. You add it manually, just like any other source file.
However, before you do that please go through the documentation on SPM. It could help you to understand some specifics (like where do the dependencies end up in your repo once you check them out, how to arrange your own code so that SPM will handle it correctly, which folders to create, etc).
Using Swift Package Manager (SPM) for Existing [iPhone App] XCode Project (Having Swift + Objective C Files)
As far as I know you can't use SPM right now within iOS apps.
You can use it in hosted environments for example if you are hosting a web application with Vapor on a Mac or on Linux.
Specify minimum macOS version for Swift Package Manager with Swift 5
let package = Package(
name: "NAME",
platforms: [
.macOS(.v10_11)
],
products: [
.library(name: "NAME", targets: ["NAME"]),
],
targets: [
.target(name: "NAME"),
]
)
One way to do this is with Deployment Settings in SPM.
Related Topics
Uitesting Xcode 7: How to Tell If Xcuielement Is Visible
How to Remove Numbers from a String in Swift
Swift: Loop Over Array Elements and Access Previous and Next Elements
Firebase Firestore Not Updating Email Verification Status
How to View Value of Swift "Let" Constant in Xcode 6 Debugger
Check for Value or Reference Type in Swift
Understanding Shorthand Closure Syntax for Map Function in Swift
Handle Swiftui and Corelocation with Mvvm-Pattern
Drawing a 3D Arc and Helix in Scenekit
How to Delete from Firebase Database
Swift: Does Closure Have References to Constants or Variables
Undefined Behavior, Or: Does Swift Have Sequence Points
Xcode 8.2.1 Not Showing Documentation Description on Autocomplete
How to Handle Multiple Network Call in Alamofire