【问题标题】:how to write code on swiftui project to add array field to Cloud Firestore如何在 swiftui 项目上编写代码以将数组字段添加到 Cloud Firestore
【发布时间】:2021-07-17 06:16:00
【问题描述】:

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

如何编写代码将类型(数组)的数据添加到项目中,如图所示

add array field to firebase cloud

在 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

在 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())
    
  }
}

编辑:

最后,我能够整理出我想要的流派列表,但是如何选择其中的两个或更多,然后将它们存储在 Firebase 的 [array] 中?

这是我修改的内容:

在 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

【问题讨论】:

  • 欢迎来到 SO。一般来说,您应该展示您编写的一些代码(不是代码链接),然后展示您遇到问题的地方:How to Ask。在这种情况下,你需要知道如何在 Swift 中定义一个 Array 吗?
  • 这里不知道怎么写swift代码
  • 如何在 Stack Overflow 上编写 Swift 代码?您可以将其粘贴到问题中 - 有格式化程序可以帮助您在编辑器中包含 { } 按钮。
  • 我做到了,谢谢
  • 我看到您已经编辑了问题,但是您现在的问题是什么?

标签: arrays xcode firebase google-cloud-firestore swiftui


【解决方案1】:

要添加与您的 Firestore 结构匹配的数组,您可以将其添加到您的 Card 模型中:

var genress : [String]

您还需要将该参数添加到您的testData —— Xcode 应该给您一个带有“修复”选项的错误,该选项将为您提供一个自动完成来修复它并给它一个参数。例如,您可以只使用genress: []genress: ["item1", "item2"]

顺便说一句,“genress”看起来可能是拼写错误。如果是这样,您需要在 Firestore 中更改您的模型(也许您的意思是“流派”?)。

【讨论】:

  • 是的,我的意思是“流派”,我现在就试试
  • 效果很好,但是如何在流派字段中输入数据,所以它不是预定义的数据?例如,我有一个 BBC 电台广播什么流派: News Songs Dialogues Other (Write) 也就是说,species 是作为一个下拉列表并且选择了多个。
  • 没有真正的内置 SwiftUI 组件。您可以在List 中显示可用项目,然后跟踪哪些项目被选中。您可以拥有Toggles 的列表并打开或关闭它们等。看看这个问题/答案:stackoverflow.com/questions/57022615/…
  • 其实这个链接很有用,但是不知道怎么放到我的代码里?我应该在哪里调整代码?如何用这样的列表替换流派字段? i.stack.imgur.com/YCyKc.png
  • 看代码。看看他们在哪里有“苹果”、“橙子”等?用你的流派替换那些。
猜你喜欢
  • 2020-04-25
  • 2018-06-22
  • 1970-01-01
  • 1970-01-01
  • 2019-12-17
  • 2010-12-17
  • 1970-01-01
  • 2019-04-01
  • 2019-07-18
相关资源
最近更新 更多