arrays - 当使用带有一系列选项的 swiftui 选择器时。如何将所选选项而不是索引值保存到 Cloud Firestore?
问题描述
我希望能够访问和使用该位置。目前只有选择器中的索引值被保存到 Cloud Firestore。
这是选择器:
Picker(selection: $viewModel.injury.locationIndex, label: Text("General Location")) {
ForEach(0 ..< locationOptions.count) {
Text(self.locationOptions[$0])
}
这是数组:
var locationOptions = [" Head", " Chest", " Right Arm", " Left Arm", " Right Hand", "✋ Left Hand", " Waist", " Right Leg", " Left Leg", " Right Foot", " Left Foot"]
locationIndex 已成功上传到 Cloud Firestore:
import SwiftUI
import Firebase
class InjuryViewModel: ObservableObject {
@Published var injury: Injury = Injury(id: "", userId: "", specificLocation: "", comment: "", active: false, injuryDate: Date(), exercises: "", locationIndex: 0)
private var db = Firestore.firestore()
func addInjury(injury: Injury) {
do{
var addedInjury = injury
addedInjury.userId = Auth.auth().currentUser?.uid
let _ = try db .collection("injuries").addDocument(from: addedInjury)
}
catch {
print(error)
}
}
func save () {
addInjury(injury: injury)
}
}
解决方案
Picker 的选择和标识符必须是相同的类型。所以你会有类似的东西
Picker(selection: $viewModel.injury.specificLocation, label: Text("General Location")) {
ForEach(locationOptions, id: \.self) {
Text($0)
}
推荐阅读
- javascript - 如何在asp中从javascript jquery中获取选定的值以在C#后面的代码中使用它
- javascript - 通过 jQuery AJAX 发送的值在控制器中始终为空
- r - 为什么打印数据帧会采用另一种编码而不是打印数据帧的列?
- angular - 如何仅使用 Angular 6 中的打字稿在右下角打开 bs Modal?
- python - 如果字典中的两个值属于同一个键,它们是否属于另一个字典中的同一个键?
- groovy - Kamel 安装命令不起作用。错误:无法自动找到推送图像的注册表
- java - 找到旋转最多的轴?
- graphics - 反馈循环是否也适用于 OpenGL ES 中的缓冲区访问?
- python - 如何删除字符串中两个代码标签之间的逗号、句号和分号?
- python - 如何避免短文本分类中的实体过拟合?