How to build sample app in SwiftPM?

Hello, I'm intaek cho.

I thought SPM can include sample app and build it.
But I realized that it seems not working.

Here is my local directory.

MyPackage

  • MyPackage
  • MyPackageTests
  • MyPackageSample
    • MyPackageSample
      • ...
      • AppDelegate(or main for objc)

And here is my Package.swift.

// swift-tools-version:5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "MyPackage",
    defaultLocalization: "en",
    platforms: [.iOS(.v15)],
    products: [
        .library(
            name: "MyPackage",
            targets: ["MyPackage"]),
    ],
    targets: [
        .target(
            name: "MyPackage",
            path: "MyPackage",
        .testTarget(
            name: "MyPackageTests",
            dependencies: ["MyPackage"],
            path: "MyPackageTests",
        .executableTarget(
            name: "MyPackageSample",
            dependencies: [
                "MyPackage"
            ],
            path: "MyPackageSample/MyPackageSample"
        )
    ]
)

And I added a scheme for MyPackageSample and built it.
But I got a crash like below.

com.apple.uikit.eventfetch-thread (8): EXC_BREAKPOINT (code=1, subcode=0x189e021c0)

Is it not possible to build sample project in SPM?
Do I have to always close the SPM and reopen the sample app project(xcodeproj)?

Thanks.

Currently, you cannot build an iOS app using Swift Package Manager (Swift Playgrounds technically work and are SwiftPM but they use some special magic so I'm not counting them).

If you wan't to build an iOS app then you must use an Xcode project.

If you want to build a command line app, or terminal app, or plain Mac app (no UI) then you can use a SwiftPM app - this is how all Swift server apps are built

2 Likes

Huge Thanks for the reply.
Is there a reference about that by any chance?

Reference for which bit?

About spm is not for building UI-based app, or executableTarget is not for a sample app.

I believe the issue is that an MyApp.xcodeproj file is needed for Xcode to run the app. This includes the many build settings, configs, etc. It's possible that SPM will someday support all those settings, but not yet.

One thing you can do is have an Xcode project, but have literally all your code in an SPM package. inside the project.

1 Like

A workflow that I use consistently is to use an Xcode Workspace that contain the Swift package and the Xcode project for the demo app. Then I add the Swift package as a local package to the Xcode project. This way I have schemes for both and I can easily work on the package while the demo app scheme is active to instantly spot issues with the package’s public interface.

Xcode 16 seems to like this setup better than opening the Xcode project and the package in separate windows.

I can even host the package and the Xcode project in separate Git repos and commit to each repo individually using Xcode’s UI.

1 Like