Importing modules with Swift package manager

我是研究僧i 提交于 2019-12-13 03:54:45

问题


I am trying to use Swift's package manager to import external modules in my project. My first module come from the Vapor project. I cannot seem to get it working. I start with

swift package init
swift package generate-xcodeproj

My Package.swift looks like this:

import PackageDescription

let package = Package(
    name: "OpenTools",
    products: [
        .library(
            name: "OpenTools",
            targets: ["OpenTools"]),
    ],
    dependencies: [
        .package(url: "https://github.com/vapor/json.git", from: "2.0.0")
    ],
    targets: [
        .target(name: "OpenTools", dependencies: ["JSON"]),
    ]
)

I then run

swift package update
swift package generate-xcodeproj # to regenerate with dependencies

and then try to import the JSON package in my main file

import JSON

The modules are there as shown below but the import gets back with an No such module 'JSON' error.

Any thoughts?


回答1:


If I had enough reputation, I would formulate this as a comment. ;)
Probably the problem lies within Xcode, as it does not know yet, that JSON exists, because it was not built yet. This can easily be solved by just building your project (with cmd-B). With the generated xcodeproj, Xcode should know, that it first needs to build JSON and then the rest, because JSON is marked as a dependency for your target.
You can check this, by navigating in Xcode to your target (when you click on the project description file) and afterwards to "Build Phases". Under Target Dependencies you should find your JSON module.

In addition you should find a JSON module under your targets, which compiles the sources you gathered from github.

Your project should also build when executing swift build in your project root.




回答2:


With Xcode 11 you should be able to open Package.swift directly which will give you a proving ground for verifying the package manifest (aka: the Package.swift file) and compiling the target. This should help see what is actually causing the error that's preventing the module from being compiled.



来源:https://stackoverflow.com/questions/47268035/importing-modules-with-swift-package-manager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!