Enes F.
Enes F.

Reputation: 535

Xcode - SwiftUI Preview Inside SPM Package Error "Could not find host for previews"

I am using a SPM package and added one simple DashboardView inside the package as below:

import SwiftUI

struct DashboardView: View {
    var body: some View {
        Text("Hello World")
    }
}

struct DashboardView_Previews: PreviewProvider {
    static var previews: some View {
        DashboardView()
    }
}

When I try to preview DashboardView, I get error:

Cannot preview in this file

Could not find host for previews

Preview Error Detail

Tried on Xcode 13.4.1, 14.0.1, 14.1_beta_3 but error is same.

Upvotes: 4

Views: 910

Answers (1)

Enes F.
Enes F.

Reputation: 535

In Package.swift file of my SPM Package, I just removed the line below which defines library type as static and error is gone.

let package = Package(
...

products: [
    // Products define the executables and libraries a package produces, and make them visible to other packages.
    .library(
        name: "MyKit",
        type: .static, // <- This line removed
        targets: ["MyKit"]
    ),
]

...

Upvotes: 2

Related Questions