Here is how you use Swift package manager in your XCode app projects
The workflow:
- Terminal:
cd ~/dev/MyProject/
👈 navigate to your project - Terminal:
swift package init
👈 creates the initial SPM files - Add the code from: The Package.swift file paragraph to your newly created Package.swift file:
- Terminal:
swift build
👈 downloads the dependencies from github and builds binaries (aka .framework) - Terminal:
swift package generate-xcodeproj
👈 Creates an XCode project that has .framework files - XCode: Open MyProject.xcodeproj and then: file -> Target -> Cocoa app -> Name it MyApp
- XCode: Select MyProject -> General -> Targets -> MyApp -> Embedded binaries -> Add the Utils.framework
- XCode: Add:
@testable import Utils
toAppDelegate.swift
andprint(StringParser.sansSuffix("blue"))
inside theapplicationDidFinishLaunching
method cmd + shift + k
to clean your xcode. This is sometimes a needed step if the import statement doesn’t work- XCode: Run the MyApp and it will now print
blu
The Package.swift file:
/*basically adds Swift-utils as a third Party framework in your project */
import PackageDescription
let package = Package(
name: "MyProject",
dependencies: [
.Package(url: "https://github.com/eonist/swift-utils.git", Version(0, 0, 0, prereleaseIdentifiers: ["alpha", "3"]))
]
)
Why is this awesome?
- When you develop your app. You use binaries but you have full access to third-party source code. 🔑
- When you need to download a new version of third party libs: Terminal:
swift package update
- Supports nested frameworks 👈 “holy grail of Dependency management”
- Supports CI, ✅ this
- If you are developing on your own third-party modules you can integrate this workflow with no extra steps. as the packages are pure .git projects. Just commit back to github repo directly from inside your project. Either by
git checkout master
orgit push origin HEAD:master
- If you add code to third-party dependencies then they are embedded in the binaries automatically.
Daily driver:
It takes a bit of time to setup. But as your daily driver this is an extremely efficient way to develop apps. As you can update multiple repos from 1 single xcode project. You really only set this up at the start of the app and never again. personally I code for 1 app project but in that same app project i update and commit to maybe 20 other repos on github in various levels of the dependency hierarchy. It makes fixing small libraries really fast and you can do 360 turns really quick when you need to. You can also pretty effortlessly split repos into ever smaller repos making things modular. Its also faster to build this way, as you don’t have to keep rebuilding parts of the code that hasn’t changed. Xcode can build complex apps with 100k LOC in a matter of seconds. As appose to many minutes if you keep the code as pure source.
Todo:
- Investigate
buildMetadataIdentifier
👈 Supposedly it enables you to target single commit ids rather than version tags - Implement CI Travis
- C - code in SPM: here