简体   繁体   English

使用Swift Package Manager添加依赖项

[英]Adding a dependency using Swift Package Manager

I am trying to add Socket.IO to my Xcode project. 我正在尝试将Socket.IO添加到我的Xcode项目中。 I am quite new to Swift Package Manager ( Where are the days where we could just drop code in our project ) 我是Swift Package Manager的新手( 在哪里可以将代码放入项目中的日子

I created a Package.swift file containing the following as described here 我创建了一个Package.swift包含下列内容的文件描述在这里

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. 然后,我打开终端并导航到包含项目和Package.swift文件的文件夹。 (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( ^ 错误:清单分析错误:错误:调用中的参数标签不正确(具有“名称:产品:目标:依赖关系:”,预期的“名称:pkgConfig:目标:依赖关系:”)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): 由于从github页面获取的原始示例没有这样的标签设置,因此每次尝试添加包管理器建议的内容时,我都将代码更改为以下内容(将一个参数放在另一个参数之前,添加其他参数):

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 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. 它将Package.resolved文件放置在我的项目文件夹中。 I cannot find a reference to what this file is and I am still lost with regards to using Socket.IO in my project. 我找不到该文件的引用,而在我的项目中使用Socket.IO仍然迷失了方向。

EDIT 2 编辑2

I proceeded in using Carthage to import the frameworks. 我开始使用Carthage导入框架。 Got it setup in 15 minutes. 在15分钟内完成设置。 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. 如果仍然有人可以回答以上问题,请随时参考,我将继续对其进行测试,并接受其是否有效。

您需要将此行添加到Package.swift的顶部

// swift-tools-version:4.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM