mica
mica

Reputation: 4308

Xcode produces "Segmentation fault: 11" compiling

Compiling the following code, Xcode Version 11.3.1 (11C504) throws:
"error: Segmentation fault: 11 (in target 'TestDel3' from project 'TestDel3') "

If I comment out Image(systemName: systemName) and replace it with Image(systemName: "person") it compiles

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?

Upvotes: 1

Views: 401

Answers (1)

Asperi
Asperi

Reputation: 257693

Update: the following variant works

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
  }
}

Initial (just in case):

Taking into account that SwiftUI uses almost everywhere opaque types by some View, the approach with erasing type for label compiled & run well (tested with Xcode 11.3.1 / iOS 13.3). I'm not sure about your needs to have explicit type here, but just be aware.

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
  }
}

Upvotes: 1

Related Questions