Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use swift package manager on existing xcode project

Tags:

im new to the swift and xcode world, so i'm having a problem trying to integrate a package to my project.

I want to add Alamofire dependency, with the following commands:

Inside my root project folder:

swift init

this creates the Package.swift file, i add the dependency inside, run then:

swift build

Everything seems to be ok, but im my project when i try to import my library:

import Alamofire

I get an error, it says that the module is not recognized. So my question here is, what is the correct steps to integrate Package Manager and a dependency on a existing project without crashing everything.

UPDATE:

swift build

outputs:

Resolved version: 4.3.0
Compile Swift Module 'Alamofire' (17 sources)
Compile Swift Module 'Sample' (1 sources)

And my Package.swift is:

import PackageDescription

let package = Package(
    name: "Sample",
    dependencies: [
        .Package(url: "https://github.com/Alamofire/Alamofire.git", majorVersion: 4)
    ]
)
like image 413
Jesús Cota Avatar asked Jan 27 '17 18:01

Jesús Cota


People also ask

Can you use Swift package manager and Cocoapods?

Cocoapods and SwiftPM are some of the most popular package managers. By supporting both in your open-source library, you potentially increase the developers who will use it. 🚨Remember the most important thing all package managers want to know is where are the source files in order to distribute them.


2 Answers

If you're using an Xcode project, you don't need (and shouldn't use) a Package.swift, just click the plus icon in Swift Packages in Xcode, and add the GitHub URL of the Swift Package, and the library will also be added to your target automatically (follow the GIF below, or click Add icon in image here):

Extra info

  • Inconsistency problem: You can't maintain both an Xcode project and a Swift.package for the same targets. They do not synchronize, and will become inconsistent, so depending on which tools, you'll get different builds: confusing. You used to be able to create a xcodeproj based on Package.swift using swift package generate-xcodeproj, but this is deprecated. Changes you make to this Xcodeproj didn't get reflected in the original Package.swift).
  • xcodebuild vs. swift build: Conveniently, if there is no xcodeproj in the same directory as your Package.swift, xcodebuild will auto-generate schemes for you to use, so you don't have to use swift build. For example, run xcodebuild -list to see the list of schemes generated from your Package.swift file, then use one of these schemes. Unconveniently, there isn't a way/ config to make xcodebuild use Package.swift.
like image 21
Ben Butterworth Avatar answered Sep 22 '22 14:09

Ben Butterworth


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.

like image 96
Eneko Alonso Avatar answered Sep 22 '22 14:09

Eneko Alonso