Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 704
Enes F. Avatar asked Oct 15 '25 21:10

Enes F.


1 Answers

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"]
    ),
]

...
like image 104
Enes F. Avatar answered Oct 17 '25 16:10

Enes F.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!