[英]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())
}
}
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())
}
}
} }
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"?).
如果是这样,您需要在 model和Firestore 中更改它(也许您的意思是“流派”?)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.