简体   繁体   English

如何在 swiftui 项目上编写代码以将数组字段添加到 Cloud Firestore

[英]how to write code on swiftui project to add array field to Cloud Firestore

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:我是 Xcode 编程的初学者,我从这个站点下载了一个项目( https://www.raywenderlich.com/11609977-getting-started-with-cloud-firestore-and-swiftui ),它允许您添加数据到firestore数据库,我在项目中成功添加了一些字段,但是遇到了不知道要解决的问题:

How to write code to add data of type (Array) to the project, as is shown in the picture如何编写代码将(Array)类型的数据添加到项目中,如图

add array field to firebase cloud将数组字段添加到 firebase 云

on card.swift卡上。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在 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?最后,我能够将我想要的流派列表放在一起,但是我怎样才能选择其中两个或更多,然后将它们存储在 Firebase 的 [array] 中?

Here is what I have modified:这是我修改的内容:

in NewCardForm.swift在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截屏

To add an array that matches your Firestore structure, you can add this to your Card model:要添加与您的 Firestore 结构匹配的数组,您可以将其添加到您的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.您还需要将该参数添加到您的testData中——Xcode 应该会给您一个带有“修复”选项的错误,该选项将为您提供一个自动完成来修复它并给它一个参数。 You could, for example, use just genress: [] or genress: ["item1", "item2"] .例如,您可以只使用genress: []genress: ["item1", "item2"]

By the way, "genress" looks like it could be a misspelling.顺便说一句,“genress”看起来可能是拼写错误。 If so, you'd need to change it both your your model and in Firestore (maybe you meant "genres"?).如果是这样,您需要在 modelFirestore 中更改它(也许您的意思是“流派”?)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将一组自定义对象添加到Firestore文档字段? - How do I add an array of custom objects to a Firestore document field? 使用带有一系列选项的 swiftui 选择器时。 如何将所选选项而不是索引值保存到 Cloud Firestore? - When using a swiftui picker with an array of options. How can I save the selected option rather than the index value to Cloud Firestore? Cloud Firestore 读取数组 - Cloud Firestore Reading an Array 如何在 php 代码中的 mysqli 数组中添加“排序依据”字段,然后按降序排序? - How to add a “sort by” field to mysqli array in php code and then sort by it descending? Flutter - 如何有条件地添加或更新 Firestore 中的数组? - Flutter - How to conditionally add to or update an array in firestore? 如何将数组添加到 Firebase Firestore Swift - How to add an array to Firebase Firestore Swift 如何在 firebase firestore 的数组中添加 map 以做出反应? - How to add a map in array in firebase firestore in react? flutter-firestore:如何检索 map 字段和数组字段 - flutter-firestore: how to retrieve map field and array field 如何从Cloud Firestore中的数组获取一个字符串? - How to get one string from an array in a Cloud Firestore? firestore 集合中的字段数组中可以存储多少个 id - How many ids can be stored in a field array in firestore collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM