无法从自定义框架呈现SwiftUI预览

时间:2019-12-02 10:52:33

标签: ios swift xcode swiftui

我正在尝试在自定义框架中编写SwiftUI视图。

之所以这样做是因为我想隐藏子视图的实现,因为Swift没有提供命名空间。因此,我做了一个子框架,并在其中实现了测试视图。

所以我想做

  1. 制作一个应用(名称:singleview)
  2. 制作一个子模块(名称:Framework,名为Calendar
  3. 导出视图(名称:单元格)
  4. 两者都预览(*)

然后我得到了

  1. 构建并成功运行
  2. 我可以看到应用程序ContentView的预览

但是问题是:

  • 我无法看到Cell的预览(在框架Calendar内部),并显示以下错误: img1
'Cell_Previews' is not a member type of 'Calendar'

----------------------------------------

BuildError: Failed to build Cell.swift

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

failedToBuildDylib: /Users/q/Library/Developer/Xcode/DerivedData/singleview-cmqdlfabgiintkgnvdpzzagojvzf/Build/Intermediates.noindex/Previews/singleview/Intermediates.noindex/Calendar.build/Debug-iphonesimulator/Calendar.build/Objects-normal/x86_64/Cell.2.preview-thunk.swift:22:36: error: 'Cell_Previews' is not a member type of 'Calendar'
typealias Cell_Previews = Calendar.Cell_Previews
                          ~~~~~~~~ ^
/Users/q/Library/Developer/Xcode/DerivedData/singleview-cmqdlfabgiintkgnvdpzzagojvzf/Build/Intermediates.noindex/Previews/singleview/Intermediates.noindex/Calendar.build/Debug-iphonesimulator/Calendar.build/Objects-normal/x86_64/Cell.2.preview-thunk.swift:6:6: error: replaced accessor for 'previews' could not be found
    @_dynamicReplacement(for: previews) private static var __preview__previews: some View {
     ^
/Users/q/Library/Developer/Xcode/DerivedData/singleview-cmqdlfabgiintkgnvdpzzagojvzf/Build/Intermediates.noindex/Previews/singleview/Intermediates.noindex/Calendar.build/Debug-iphonesimulator/Calendar.build/Objects-normal/x86_64/Cell.2.preview-thunk.swift:21:27: error: 'Cell' is not a member type of 'Calendar'
typealias Cell = Calendar.Cell
                 ~~~~~~~~ ^
/Users/q/Library/Developer/Xcode/DerivedData/singleview-cmqdlfabgiintkgnvdpzzagojvzf/Build/Intermediates.noindex/Previews/singleview/Intermediates.noindex/Calendar.build/Debug-iphonesimulator/Calendar.build/Objects-normal/x86_64/Cell.2.preview-thunk.swift:14:6: error: replaced accessor for 'body' could not be found
    @_dynamicReplacement(for: body) private var __preview__body: some View {
     ^

那么,如果我想查看预览,该怎么办?

img2 这是该项目的屏幕截图

img3 这是失败的屏幕截图

我正在使用Xcode 11.2.1(11B500)。

谢谢!


编辑:谢谢克里斯! Cell.swift(在框架内部)的代码是:


import SwiftUI

public struct Cell: View {
    public init() {}

    public var body: some View {
        Text("INTERNAL")
    }
}

public struct Cell_Previews: PreviewProvider {
    public init() {}

    public static var previews: some View {
        Cell()
    }
}

ContentView.swift(框架的用户)为:

import SwiftUI
import Calendar

struct ContentView: View {
    var body: some View {
        Cell()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

0 个答案:

没有答案