Search code examples
iosswiftpackage-managers

Adding a dependency using Swift Package Manager


I am trying to add Socket.IO to my Xcode project. I am quite new to Swift Package Manager (Where are the days where we could just drop code in our project)

I created a Package.swift file containing the following as described here

import PackageDescription

    let package = Package(
        name: "socket.io-test",
        products: [
            .executable(name: "socket.io-test", targets: ["MyTarget"])
        ],
        dependencies: [
            .package(url: "https://github.com/socketio/socket.io-client-swift", .upToNextMinor(from: "14.0.0"))
        ],
        targets: [
            .target(name: "MyTarget", dependencies: ["SocketIO"], path: "./My/Target/Path/")
        ]
    )

I then opened my terminal and navigated to my folder containing both my project and my Package.swift file. (I am assuming it should be in my project folder)

I then ran the following command:

swift package resolve

This produced the following error:

error: manifest parse error(s): error: argument 'targets' must precede argument 'dependencies' targets: [ ~~~~^~~~~~~~~~

So, following the instruction given by the error, I moved my 'targets' above dependencies.

I ran the command again and I then received the following error:

error: manifest parse error(s): error: incorrect argument label in call (have 'name:products:targets:dependencies:', expected 'name:pkgConfig:targets:dependencies:') let package = Package( ^

Since the original example taken from the github page did not have the labels setup like this, I simply changed my code to the following, each time trying to add what the package manager suggested (Preceding one argument to another, adding additional arguments):

import PackageDescription

let package = Package(
    name: "socket.io-test",
    pkgConfig: nil,
    providers: nil,
    targets: [
        .target(name: "MyTarget", dependencies: ["SocketIO"], path: "./My/Target/Path/")
    ],
    dependencies: [
        .package(url: "https://github.com/socketio/socket.io-client-swift", .upToNextMinor(from: "14.0.0"))
    ],
    swiftLanguageVersions: nil,
    exclude: nil,
    products: [
        .executable(name: "socket.io-test", targets: ["MyTarget"])
    ]
)

However, now the package manager is telling me the following strange error:

error: manifest parse error(s): error: extra argument 'products' in call products: [ ^

So now I give up, as I am clearly getting nowhere. How can the 'products' argument not belong here? And why does nothing suggested by the package manager work? I am sure I am missing something small. Can someone please point me in the right direction?

I have also looked at the following websites for assistance, but still cannot get mine to work.

Swift.org https://medium.com/xcblog/apple-swift-package-manager-a-deep-dive-ebe6909a5284

Hope my question isn't too long and that someone will find the time to assist :)

EDIT

I changed the code to the following:

import PackageDescription

let package = Package(
    name: "socket.io-test",
    dependencies: [
    .Package(url: "https://github.com/socketio/socket.io-client-swift", majorVersion: 14)
    ]
)

And this seemed to have worked without errors. It placed a Package.resolved file in my project folder. I cannot find a reference to what this file is and I am still lost with regards to using Socket.IO in my project.

EDIT 2

I proceeded in using Carthage to import the frameworks. Got it setup in 15 minutes. As the is a different solution not relevant to my question, I decided to add it as an edit rather than an answer.

If anyone is still able to answer the above, please feel free for future reference and I will still test it, and accept if its working.


Solution

  • You need to add this line to the top of your Package.swift

    // swift-tools-version:4.2