Import Local Swift Package in Xcode 13.3

Hello!

I am struggling with the most basic things :-(

I created a Swift Package with the wizard and left it basically untouched, only added a func:

public struct TestPackage {

    public init() {

    }

    public static func hello()
    {
        print("Hello, World!")
    }
}

It just build fine.

Now I created a second project, this time a SpriteKit iOS app. In the file list on the left, right-clicked on "Packages" -> "Add Packages ..." -> "Add Local" and selected the root directory of TestPackage (containing the Package.swift) The Package now correctly appears in the "Packages" folder.

Opening a random Swift file in said SpriteKit iOS app, I expect to be able to

import TestPackage

But it tells me "No such module TestPackage".

Searching the internet, I somewhere read that I have to add the package also to the "Frameworks, Libraries and Embedded Content" section in the project's target settings, but I can't. Hitting the "+" button there does not give me my library in the list of suggested libraries. Via "Add Other" -> "Add package dependency" -> "Add Local ..." I can again select the root directory of my library, but other than showing up twice in the left-side folder view (and not in said Frameworks, Libraries and Embedded content", I have no luck in importing it to my code.

What am I doing wrong?

Accepted Reply

It's a two step process. Load the package locally, then add the framework manually. Adding a remote package does both steps at once. Adding a local package does not.

Replies

In the "General" section of the Project setup, did you add "TestPackage" as a Framework? I had to do this for my local packages

Yes, but I always get to the same dialog presenting me all Xcode-internal Swift packages. If I hit the "Local ..." button at the bottom, I can again select the package from Finder, and a duplicate of the package shows up in the left-hand folder view under "Packages", nowhere else. The problem persists.

Either Apple doesn't want me to use local packages (which would be kinda stupid) or this is a serious bug. Can't believe it is THAT hard to include local Swift packages. I am running Xcode 13.3.

What you did is correct. I'm talking about a different place to enter the framework. In Project->General->Target you'll see a list. Identity.. Deployment Info... Frameworks and Libraries.. In here hit the +. Your local library should appear as an option and click it. I tried to insert a photo of what I'm talking about. Couldn't do it. Sorry

It's a two step process. Load the package locally, then add the framework manually. Adding a remote package does both steps at once. Adding a local package does not.

Thank you ronm for the great help!

As I found out now, it seems Xcode needs the Package being a Git repo in order to properly work, and the repo root directory has to be the root directory also for the package. (not in a sub-directory) As for now, this is fine with me, as I plan to version-control the package anyway.

I first added the package via General -> Project -> Package Dependencies -> "+" Button And then added it on the target as you suggested via General -> Target(s) -> Frameworks, Libraries and Embedded Content.

AFAICT it's actually just a one step process.

You can add to the target via General -> Target(s) -> Frameworks, Libraries and Embedded Content, and you're good to go.

I don't think you need the add via General -> Project -> Package Dependencies -> "+" step at all. Perhaps this is for local packages that have a local git repo but aren't part of the existing project.