2

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.

Eventually the buttonclick will access a database to build an array with more row, but for now I just trying to add one row.

Code: enter image description here

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&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()
  }
}
2
  • 1
    Like the error says, the append function is only expecting there to be 1 argument passed in. You probably meant to pass in an object containing the id, code, and title. Commented Feb 19, 2020 at 19:36
  • Can you suggest the statement. Commented Feb 19, 2020 at 20:53

1 Answer 1

7

You need to push the object rather than 3 values

push_group_array.append(push_group_row(id: 1, code: newCode, title: newTitle))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.