How to: Previews in Swift Package?

How do you get previews working in a swift package? This talk shows this working, however the following steps do not work.
  1. Using Xcode 12 create a new Swift Package

  2. Add a simple view with a preview to that swift package (See code below)

  3. Attempt to run the preview.

  4. See errors


Code:
Code Block
#if os(iOS)
import SwiftUI
@available(iOS 14.0, *)
public struct TestView: View {
    public init() {        
    }
    public var body: some View {
        Text("Hello World")
    }
}
@available(iOS 14.0, *)
struct NoDataView_Previews: PreviewProvider {
    static var previews: some View {
        TestView()
    }
}
#endif


Error:
Code Block
Build input file cannot be found:...TestPackagePreview.o' (in targ
SchemeBuildError: Failed to build the scheme "TestPackagePreview"
Build input file cannot be found: ...TestPackagePreview.o' (in target 'TestPackagePreviewTests' from project 'TestPackagePreview'
Link TestPackagePreviewTests (x86_64):
error: Build input file cannot be found: ...TestPackagePreview.o' (in target 'TestPackagePreviewTests' from project 'TestPackagePreview')


Note I have added "..." in file paths for privacy reasons

Accepted Reply

The solution:
  1. Create a new swift package

  2. Delete the auto generated struct that will be the name of your swift package

  3. Remove the unit test associated with the auto generated struct

  4. Edit the package.swift file to include the platform (example below: iOS)

  5. Ensure you have the schema selected and the correct device selected for the preview you want. (Ex: iPhone selected for iOS)

Code Block
    platforms: [
            .iOS(.v13)
        ],

Replies

Try to remove default TestPackagePreview struct that was added by Xcode in TestPackagePreview target. Looks like Xcode 12 somehow makes an alias to this struct when it builds preview which leads to a circular dependency.

This is the error I had under "Diagnostics" button ("MyLibrary" is the name of the package and also the name of this default struct):
Code Block
.../Build/Intermediates.noindex/Previews/MyLibrary/Intermediates.noindex/MyLibrary.build/Debug-iphonesimulator/MyLibrary.build/Objects-normal/x86_64/MyLibrary.2.preview-thunk.swift:13:11: error: circular reference
typealias MyLibrary = MyLibrary.MyLibrary
          ^

Hope this helps.
@vox Humana This does not work. The error I posted above is from the "Diagnostics" button, which remains after your proposed fix. Have you gotten previews to work in a swift package yourself?
The solution:
  1. Create a new swift package

  2. Delete the auto generated struct that will be the name of your swift package

  3. Remove the unit test associated with the auto generated struct

  4. Edit the package.swift file to include the platform (example below: iOS)

  5. Ensure you have the schema selected and the correct device selected for the preview you want. (Ex: iPhone selected for iOS)

Code Block
    platforms: [
            .iOS(.v13)
        ],