title: SWIFT UI add element to array
body: <p>I trying to learn the new SwiftUI coding technique. I would like to click a button that will add elements to an array that is a @State variable. The button is the buttonclick function.  The array is the push_group_row / push_group_array.    I get an error in the append statement. </p>
  
  <p>Eventually the buttonclick will access a database to build an array with more row, but for now I just trying to add one row.</p>
  
  <p>Code:
        <a href="https://i.stack.imgur.com/7UIsa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7UIsa.png" alt="enter image description here"></a></p>
  
  <pre><code>import SwiftUI
  import Combine
  
  var gordon: String = "xxxxxx"
  
  struct Result: Codable {
    let trackId: Int
    let trackName: String
    let collectionName: String
  }
  struct Response: Codable {
    var results: [Result]
  }
  struct Pokemon: Identifiable {
    let id: Int
    let name: String
    let type: String
    let color: Color
  }
  struct push_group_row {
    let id: Int
    let code: String
    let title: String
  }
  
  struct ContentView: View
      {
      @State private var results = [Result]()
      @State var pokemonList = [
         Pokemon(id: 0, name: "Charmander", type: "Fire", color: .red),
         Pokemon(id: 1, name: "Squirtle", type: "Water", color: .blue),
         Pokemon(id: 2, name: "Bulbasaur", type: "Grass", color: .green),
         Pokemon(id: 3, name: "Pikachu", type: "Electric", color: .yellow),]
      @State var push_group_array = [push_group_row(id: 0, code: "code12", title: "POAFire")]
  
      var body: some View
      {
      NavigationView
      {
          VStack(alignment: . leading){
          Button(action: {
              // What to perform
              self.buttonclick()
          }) {
              // How the button looks like
              Text("clickme")
              .background(Color.purple)
              .foregroundColor(.white)
          }
  
          List(results, id: \.trackId)
          {item in
              NavigationLink(destination: DetailView(lm: String(item.trackId)))
              {
              VStack(alignment: .leading)
              {
                  Text(String(item.trackId))
                  Text(item.trackName)
                      .font(.headline)
                  Text(item.collectionName)
                  Text(gordon)
              }
              }
              }
  
              List(self.pokemonList)
              { pokemon in
                  HStack
                      {
                      Text(pokemon.name)
                      Text(pokemon.type).foregroundColor(pokemon.color)
                      }
  
              }
  
              List(push_group_array, id: \.id)
                   { pg_item in
                       HStack
                           {
                           Text(String(pg_item.id))
                           Text(pg_item.code)
                           }
  
                   }
              .onAppear(perform: self.loaddata)
              }
              .navigationBarTitle("x")
              .navigationBarItems(
                  trailing: Button(action: addPokemon, label: { Text("Add") }))
  
              Spacer()
      }
  }
  
      func addPokemon() {
          if let randomPokemon = pokemonList.randomElement() {
              pokemonList.append(randomPokemon)
          }
      }
  
      // *************************** below is the add arrat code
      func buttonclick() {
          let newCode = "First"
          let newTitle = "Second"
          push_group_array.append(id: 1, code: newCode,  title: newTitle)
      }
  
      func loaddata()
      {
          print("loaddata")
  
          guard let url = URL(string: "https://itunes.apple.com/search?term=taylor+swift&amp;entity=song")
          else
          {
             print("Invalid URL")
             return
          }
  
          var urlData: NSData?
          let request = URLRequest(url: url)
          URLSession.shared.dataTask(with: request) { data, response, error in
              if let data = data
              {
                  if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data)
                  {
                  DispatchQueue.main.async
                  {
                      urlData = data as NSData?
                      self.results = decodedResponse.results
                      print(self.results)
                      print(urlData ?? "urlData_Defaultvalue")
                  }
                  return
                  }
              }
              print("Fetch failed: \(error?.localizedDescription ?? "Unknown error")")
          }.resume()
      }
  }
  
  struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
      ContentView()
    }
  }
  </code></pre>