Search code examples
iosswiftxcodeswift-package-manager

Use swift package manager on existing xcode project


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)
    ]
)

Solution

  • 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.