Pulling apart a struct in another struct

Hi everyone, I'm trying to initialize a view that uses a another struct. I want to pull that struct apart so that I can use those parts to fill in some TextFields with some already existing data, if it exists. Here is what my code looks like:

Code Block swift
struct TeamEditView: View {
   
  var team: OlympicModel.Team
   
  @Binding var isShowing: Bool
  @State var name: String = team.name
  @State var playersArray: [String] = team.players
  @State var playerNameToAdd: String = ""
  @State var color: Color = team.color
  @State var mascot: String = team.mascot


I get the error: "Cannot use instance member 'team' within property initializer; property initializers run before 'self' is available"

Code Block swift
TextField("Team Name", text: $name)


I want to eventually put these vars into TextFields like this, so users can edit it.

I'm not sure what I should do here. Could I get some help?

Answered by OOPer in 639786022

I want to eventually put these vars into TextFields like this, so users can edit it.

Then it depends on other parts of your code.
Can you show the definition of OlympicModel.Team? And how playersArray, color and mascot are used?
Please show how TeamEditView is used also.
Accepted Answer

I want to eventually put these vars into TextFields like this, so users can edit it.

Then it depends on other parts of your code.
Can you show the definition of OlympicModel.Team? And how playersArray, color and mascot are used?
Please show how TeamEditView is used also.
Hey! Thanks for replying.

Code Block swift
struct Team {
    var number: Int
    var name: String
    var players: [String]
    var color: Color
    var mascot: String
   }


Code Block swift
struct TeamEditView: View {
   
  var team: OlympicModel.Team
   
  @Binding var isShowing: Bool
  @State var name: String = team.name
  @State var playersArray: [String] = team.players
  @State var playerNameToAdd: String = ""
  @State var color: Color = team.color
  @State var mascot: String = team.mascot
   
  var body: some View {
    VStack(spacing: 0) {
      ZStack {
        Text("Edit Team").font(.headline).padding()
        HStack {
          Spacer()
          Button(action: {
            viewModel.updateTeam(teamNumber: team.number, OlympicModel.Team(number: team.number, name: name, players: playersArray, color: color, mascot: mascot))
            self.isShowing = false
          }, label: {Text("Done")}).padding()
        }
      }
       
      Divider()
      Form {
        Section(header: Text("Event Details")) {
          TextField("Team Name", text: $name)
        }
         
      }
    }
     
  }
}


I plan on putting some more TextFields in the form to be able to create and then be able to edit a Team.
Thanks for showing your code. (You put SOLVED on my previous reply, but I guess the issue is not solved yet.)

There are some parts which still are not clear enough, but if you make the property team @State, you can get the Binding for each member of OlympicModel.Team.
Code Block
struct TeamEditView: View {
@State var team: OlympicModel.Team //<-
@Binding var isShowing: Bool
@State var playerNameToAdd: String = ""
var body: some View {
VStack(spacing: 0) {
ZStack {
Text("Edit Team").font(.headline).padding()
HStack {
Spacer()
Button(action: {
viewModel.updateTeam(teamNumber: team.number, team)
self.isShowing = false
}, label: {Text("Done")}).padding()
}
}
Divider()
Form {
Section(header: Text("Event Details")) {
TextField("Team Name", text: $team.name) //<-
}
}
}
}
}


Pulling apart a struct in another struct
 
 
Q