编译以下代码时,Xcode版本11.3.1 (11C504)抛出:
错误:分割错误: 11 (项目'TestDel3‘中的目标'TestDel3')
如果我注释掉Image(systemName: systemName)并用Image(systemName: "person")替换它,它会编译
struct ContentView: View{
var body: some View {
UseButtonContainer( b: ButtonContainer("x1",systemName:"person") {print("c1")})
}
}
struct UseButtonContainer: View{
let b : ButtonContainer
var body: some View {
Button(action: {
self.b.action();
self.extraAction()
}) { b.label}
}
func extraAction()->Void{
print("extra action")
}
}
struct ButtonContainer{
let label: VStack<TupleView<(Image, Text)>>
let action: ()-> Void
init(_ text: String, systemName: String, action: @escaping ()-> Void){
self.label = VStack{
Image(systemName: systemName) // Commenting out this line
//Image(systemName: "person") // and using this instead, it compiles
Text(text)
}
self.action = action
}
}
What's wrong here?发布于 2020-02-22 06:38:13
更新:以下变体工作
struct ButtonContainer{
let label: VStack<TupleView<(Image, Text)>>
let action: ()-> Void
init(_ text: String, systemName: String, action: @escaping ()-> Void){
self.label = VStack {
Image(systemName: "\(systemName)") // << make it string literal !!!
Text(text)
}
self.action = action
}
}初始值(以防万一):
考虑到SwiftUI几乎通过some View在任何地方使用不透明类型,为label编译并运行良好的擦除类型的方法(用Xcode 11.3.1 / iOS 13.3测试)。我不确定你是否需要在这里有明确的类型,但请注意。
struct ButtonContainer{
let label: AnyView
let action: ()-> Void
init(_ text: String, systemName: String, action: @escaping ()-> Void){
self.label = AnyView(VStack {
Image(systemName: systemName)
Text(text)
})
self.action = action
}
}https://stackoverflow.com/questions/60347176
复制相似问题