Cannot render SwiftUI preview from a custom framework

Hello, I am trying to code a SwiftUI view in a custom framework.

The reason to do this is that I want to hide sub-views' implementation, since Swift does not provide a namespace. So, I made a sub-framework and Implemented a test view inside of that.

But, inside the Framework(named Calendar), If I write these code, then I cannot see the preview. Of course I can preview outside of the Framework, for example, when I import & use this in some single-view app.

import SwiftUI

public struct Cell: View {
    public init() {}
    
    public var body: some View {
        Text("CELL for SingleView")
    }
}

struct Cell_Previews: PreviewProvider {
    init() {}
    
    static var previews: some View {
        Cell()
    }
}

Everything works good, but there is an error from the preview pane:

Compiling failed: 'Cell_Previews' is not a member type of 'Calendar'

I tried to wrap the struct with #if DEBUG then:

import SwiftUI

public struct Cell: View {
    public init() {}
    
    public var body: some View {
        Text("CELL for SingleView")
    }
}

#if DEBUG
struct Cell_Previews: PreviewProvider {
    init() {}
    
    static var previews: some View {
        Cell()
    }
}
#endif

The error is changed! into:

Compiling failed: 'Cell' is not a member type of 'Calendar'

So I wrapped both of structs:

import SwiftUI

#if DEBUG
public struct Cell: View {
    public init() {}
    
    public var body: some View {
        Text("CELL for SingleView")
    }
}

struct Cell_Previews: PreviewProvider {
    init() {}
    
    static var previews: some View {
        Cell()
    }
}
#endif

The preview worked like a magic! But obviously, it is a crazy idea since it does not work in release mode.

I suspect that this is a bug. What should I do?

I think. Actually I asked this question into stackoverflow first, but there is an other person with this problem also: ios - Cannot render SwiftUI preview from a custom framework - Stack Overflow
And this is the repo for the reproduction of this error: GitHub - inq/singleview: dummy for question
In here, ContentView.swift(the user of the framework) renders preview correctly, whereas Cell.swift(the implementation of the view) does not

I've got some more hint. When I open the intermediate file(Cell.3.preview-thunk), then:

@_private(sourceFile: "Cell.swift") import Calendar
import SwiftUI
import SwiftUI

extension Cell_Previews {
    @_dynamicReplacement(for: previews) private static var __preview__previews: some View {
        #sourceLocation(file: "/Users/q/Projects/Calendar/Calendar/Cell.swift", line: 19)
        AnyView(__designTimeSelection(Cell(), "#9226.[2].[0].property.[0].[0]"))
#sourceLocation()
    }
}

extension Cell {
    @_dynamicReplacement(for: body) private var __preview__body: some View {
        #sourceLocation(file: "/Users/q/Projects/Calendar/Calendar/Cell.swift", line: 13)
        AnyView(__designTimeSelection(Text(/*@START_MENU_TOKEN@*/__designTimeString("#9226.[1].[0].property.[0].[0].arg[0].value.[0].value", fallback: "Hello, World!")/*@END_MENU_TOKEN@*/), "#9226.[1].[0].property.[0].[0]"))
#sourceLocation()
    }
}

typealias Cell = Calendar.Cell
typealias Cell_Previews = Calendar.Cell_Previews

There is two typealiases in the end of the file. But If I put #if DEBUG flag, then they disappear. I think those two lines should be the cause and it is useless. What is these?

The same error.
Guess u've named one Class with the same name of ur Project.

Just like me :)

1 Like