ToolbarContent扩展归档编译错误问题求助
为简化新SDK技术的使用,我给ToolbarContent编写了一个扩展,并将其放入私有Swift包中。
扩展代码
public extension ToolbarContent { @available(iOS 17, macOS 13, *) @ToolbarContentBuilder func removeSharedBackground() -> some ToolbarContent { Group { if #available(iOS 26, macOS 26, *) { self.sharedBackgroundVisibility(.hidden) } else { self } } } @available(iOS 16, macOS 13, *) func addMatchedTransitionSource(id: String, in namespace: Namespace.ID?) -> some ToolbarContent { Group { if #available(iOS 26, *), let namespace { self.matchedTransitionSource(id: id, in: namespace) } else { self } } } }
项目使用示例
struct ContentView: View { @Namespace private var namespace var body: some View { VStack { Image(systemName: "globe") .imageScale(.large) .foregroundStyle(.tint) Text("Hello, world!") } .padding() .toolbar { ToolbarItem(placement: .bottomBar) { Button("Hello", systemImage: "plus") { } } // .removeSharedBackground() // 此方法可正常工作 .addMatchedTransitionSource(id: "test", in: namespace) // 此方法出现问题 } } }
错误现象
代码通过常规构建(CMD+B)可正常编译,但尝试归档(如iOS分发归档)时出现编译错误:
"opaque type descriptor for <<opaque return type of static SwiftUI.ToolbarContentBuilder.buildLimitedAvailability(SwiftUI.ToolbarContent) -> some>>", referenced from:
lazy protocol witness table accessor for type SwiftUI._ConditionalContent<<<<opaque return type of static SwiftUI.ToolbarContentBuilder.buildLimitedAvailability(SwiftUI.ToolbarContent) -> some>>>.0, <<opaque return type of static SwiftUI.ToolbarContentBuilder.buildBlock(A) -> some>>>.0> and conformance < where A: SwiftUI.ToolbarContent, B: SwiftUI.ToolbarContent> SwiftUI._ConditionalContent<A, B> : SwiftUI.ToolbarContent in SwiftUI in ContentView.o
异常点
- 该错误通常不会影响
removeSharedBackground()方法(偶尔也会触发) - 若将扩展直接放在项目内部而非外部包中,则不会出现此错误
复现步骤
- 创建项目并复制上述ContentView代码
- 添加对应私有Swift包并导入
- 模拟器构建运行正常,但归档分发时失败
我怀疑是扩展定义方式有误,尝试移除Group改用ToolbarContentBuilder但无效,恳请帮忙排查问题。
内容的提问来源于stack exchange,提问作者Meyssam

