0

I am a beginner in Xcode programming, I downloaded a project from this site (https://www.raywenderlich.com/11609977-getting-started-with-cloud-firestore-and-swiftui), that allows you to add data to the firestore database, I succeeded in adding some fields in the project, but I encountered problem that I did not know to solve:

How to write code to add data of type (Array) to the project, as is shown in the picture

add array field to firebase cloud

on card.swift

import Foundation
import FirebaseFirestoreSwift

struct Card: Identifiable, Codable {
  @DocumentID var id: String?
  var countryCode: String
  var title: String
  var logo: String
  var streamURL: String
  var desc: String
  var status: Bool = true
  var userId: String?
}

#if DEBUG
let testData = (1...10).map { i in
  Card(countryCode: "countryCode #\(i)", title: "title #\(i)", logo: "logo #\(i)", streamURL: "streamURL #\(i)", desc: "desc #\(i)")
}
#endif

on newcardform.swift

import SwiftUI

struct NewCardForm: View {
  private func addCard() {
    // 1
    let card = Card(countryCode: countryCode, title: title, logo: logo, streamURL: streamURL, desc: desc)
    // 2
    cardListViewModel.add(card)
    // 3
    presentationMode.wrappedValue.dismiss()
  }

  @ObservedObject var cardListViewModel: CardListViewModel
  @State var countryCode: String = ""
  @State var title: String = ""
  @State var logo: String = ""
  @State var streamURL: String = ""
  @State var desc: String = ""

  @Environment(\.presentationMode) var presentationMode

  var body: some View {
    VStack(alignment: .center, spacing: 30) {
      VStack(alignment: .leading, spacing: 10) {
        Text("countryCode")
          .foregroundColor(.gray)
        TextField("Enter the countryCode", text: $countryCode)
          .textFieldStyle(RoundedBorderTextFieldStyle())
      }
      VStack(alignment: .leading, spacing: 10) {
        Text("title")
          .foregroundColor(.gray)
        TextField("Enter the title", text: $title)
          .textFieldStyle(RoundedBorderTextFieldStyle())
      }
      VStack(alignment: .leading, spacing: 10) {
        Text("logo")
          .foregroundColor(.gray)
        TextField("Enter the logo link", text: $logo)
          .textFieldStyle(RoundedBorderTextFieldStyle())
      }
      VStack(alignment: .leading, spacing: 10) {
        Text("streamURL")
          .foregroundColor(.gray)
        TextField("Enter the streamURL", text: $streamURL)
          .textFieldStyle(RoundedBorderTextFieldStyle())
      }
      VStack(alignment: .leading, spacing: 10) {
        Text("desc")
          .foregroundColor(.gray)
        TextField("Enter the desc", text: $desc)
          .textFieldStyle(RoundedBorderTextFieldStyle())
      }
      Button(action: addCard) {
        Text("Add New Card")
          .foregroundColor(.blue)
      }
      Spacer()
    }
    .padding(EdgeInsets(top: 80, leading: 40, bottom: 0, trailing: 40))
  }
}

struct NewCardForm_Previews: PreviewProvider {
  static var previews: some View {
    NewCardForm(cardListViewModel: CardListViewModel())
    
  }
}

EDIT :

Finally, I was able to put together a list of genres that I want, but how can I choose two or more of them and then store them in [array] in Firebase?

Here is what I have modified :

in NewCardForm.swift

    import SwiftUI

struct NewCardForm: View {
  private func addCard() {
    // 1
    let card = Card(countryCode: countryCode, title: title, logo: logo, streamURL: streamURL, desc: desc, genres: [])
    // 2
    cardListViewModel.add(card)
    // 3
    presentationMode.wrappedValue.dismiss()
  }

  @ObservedObject var cardListViewModel: CardListViewModel
  @State var countryCode: String = ""
  @State var title: String = ""
  @State var logo: String = ""
  @State var streamURL: String = ""
  @State var desc: String = ""
  @State var items: [String] = ["talk", "news", "poem", "songs"]
  @State var selections: [String] = []
  
    @Environment(\.presentationMode) var presentationMode
  var body: some View {
        VStack(alignment: .center, spacing: 30) {
      VStack(alignment: .leading, spacing: 10) {
        Text("countryCode")
          .foregroundColor(.gray)
        TextField("Enter the countryCode", text: $countryCode)
          .textFieldStyle(RoundedBorderTextFieldStyle())
      }
      VStack(alignment: .leading, spacing: 10) {
        Text("title")
          .foregroundColor(.gray)
        TextField("Enter the title", text: $title)
          .textFieldStyle(RoundedBorderTextFieldStyle())
      }
      VStack(alignment: .leading, spacing: 10) {
        Text("logo")
          .foregroundColor(.gray)
        TextField("Enter the logo link", text: $logo)
          .textFieldStyle(RoundedBorderTextFieldStyle())
      }
      VStack(alignment: .leading, spacing: 10) {
        Text("streamURL")
          .foregroundColor(.gray)
        TextField("Enter the streamURL", text: $streamURL)
          .textFieldStyle(RoundedBorderTextFieldStyle())
      }
      VStack(alignment: .leading, spacing: 10) {
        Text("desc")
          .foregroundColor(.gray)
       // TextField("Enter the desc", text: $desc)
          .textFieldStyle(RoundedBorderTextFieldStyle())
        List {
                    ForEach(items, id: \.self) { string in
                        Text(string)
                    }
      }
    
        Button(action: addCard) {
        Text("Add New Card")
          .foregroundColor(.blue)
      }
      Spacer()
    }
    .padding(EdgeInsets(top: 10, leading: 10, bottom: 0, trailing: 20))
  }
}

struct NewCardForm_Previews: PreviewProvider {
  static var previews: some View {
    NewCardForm(cardListViewModel: CardListViewModel())
    
  }
}

}

screenshot

faleh
  • 1
  • 3
  • Welcome to SO. In general, you should show some of the code you've written (not links to codes) and then show where you're having a problem: [ask]. In this case, do you need to know how to define an Array in Swift? – jnpdx Apr 23 '21 at 04:03
  • I don't know how to write swift code here – faleh Apr 23 '21 at 04:25
  • How to write Swift code on Stack Overflow? You can paste it in the question -- there are formatters to help you include the `{ }` button in the editor. – jnpdx Apr 23 '21 at 04:29
  • i do it , thank you – faleh Apr 23 '21 at 04:44
  • I see that you have edited the question, but what's your question now then? – Jordi May 28 '21 at 13:59
  • same qustion "How to write code to add data of type (Array) to the project, as is shown in the picture" , i dont know how write code – faleh Jun 26 '21 at 22:07

1 Answers1

0

To add an array that matches your Firestore structure, you can add this to your Card model:

var genress : [String]

You'll also need to add that parameter to your testData -- Xcode should give you an error with a "Fix" option that will give you an autocomplete to fix it and give it a parameter. You could, for example, use just genress: [] or genress: ["item1", "item2"].

By the way, "genress" looks like it could be a misspelling. If so, you'd need to change it both your your model and in Firestore (maybe you meant "genres"?).

jnpdx
  • 45,847
  • 6
  • 64
  • 94
  • yes , i mean "genres" , I will try it now – faleh Apr 23 '21 at 04:51
  • It works well, but how can I enter data in genres field, so it not predefined data? For example, I have a BBC radio what genres is broadcast: News Songs Dialogues Other (Write) In other words, species are as a drop-down list and chose more than one. – faleh Apr 23 '21 at 05:18
  • There's not really a built-in SwiftUI component for that. You could display the available items in a `List` and then keep track of which are selected. You could have a list of `Toggle`s and turn them on or off, etc. Take a look at this question/answer: https://stackoverflow.com/questions/57022615/select-multiple-items-in-swiftui-list – jnpdx Apr 23 '21 at 05:22
  • Actually the link is useful, but I don't know how to put it in my code? Where am I supposed to adjust the code? How to replace genres field with such this list? https://i.stack.imgur.com/YCyKc.png – faleh Apr 23 '21 at 05:46
  • Look at the code. See where they have "Apples", "Oranges", etc? Replace those with your genres. – jnpdx Apr 23 '21 at 05:47
  • Can I write the code here? Or press answer question button? To explain my problem – faleh Apr 23 '21 at 06:13
  • Edit your original question. – jnpdx Apr 23 '21 at 06:18
  • Sorry, but I couldn't do it, I wasted about an hour useless, can you please help me? How do I replace genres field with a multiple-selection menu? – faleh Apr 23 '21 at 07:11