【发布时间】: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())
}
}
}
【问题讨论】:
-
欢迎来到 SO。一般来说,您应该展示您编写的一些代码(不是代码链接),然后展示您遇到问题的地方:How to Ask。在这种情况下,你需要知道如何在 Swift 中定义一个 Array 吗?
-
这里不知道怎么写swift代码
-
如何在 Stack Overflow 上编写 Swift 代码?您可以将其粘贴到问题中 - 有格式化程序可以帮助您在编辑器中包含
{ }按钮。 -
我做到了,谢谢
-
我看到您已经编辑了问题,但是您现在的问题是什么?
标签: arrays xcode firebase google-cloud-firestore swiftui