简体   繁体   English

使用带有一系列选项的 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?

I'd like to be able to access and use the location.我希望能够访问和使用该位置。 Currently only the index value from the picker is being saved to Cloud Firestore.目前只有选择器的索引值被保存到 Cloud Firestore。

Here is the picker:这是选择器:

Picker(selection: $viewModel.injury.locationIndex, label: Text("General Location")) {
  ForEach(0 ..< locationOptions.count) {
    Text(self.locationOptions[$0])
}

Here is the array:这是数组:

var locationOptions = ["🤕 Head", "🦺 Chest", "💪 Right Arm", "💪 Left Arm", "🤚 Right Hand", "✋ Left Hand", "🩲 Waist", "🦵 Right Leg", "🦵 Left Leg", "🦶 Right Foot", "🦶 Left Foot"]

The locationIndex is successfully uploaded to Cloud Firestore: 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)
    }
    
}

The selection and identifier of Picker have to be same type. Picker 的选择和标识符必须是相同的类型。 So you would have something like所以你会有类似的东西

Picker(selection: $viewModel.injury.specificLocation, label: Text("General Location")) {
  ForEach(locationOptions, id: \.self) {
    Text($0)
}

暂无
暂无

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

相关问题 尝试使用选择器中选择的索引值而不是选择器的值 - Attempting to use value of index selected in picker rather than value of picker 如何显示微调器中的选项,将其选中后将其另存为整数变量? - How do I show options in a spinner that, when selected, save that option as an integer variable? 如何将数组索引为1而不是0? - How do I index the array to start as 1 rather than 0? 我如何对数组进行升序而不是随机排序 - How can i sort an array ascending rather than random array.Max();对于数组索引而不是值 - array.Max(); for the array index rather than the value 如何在 swiftui 项目上编写代码以将数组字段添加到 Cloud Firestore - how to write code on swiftui project to add array field to Cloud Firestore 按值而不是索引对 Perl 数组进行切片的惯用方法 - Idiomatic way to slice a Perl array by value rather than index PHP我无法将文本框上的值保存到数组中的值不止一个,但我还没有做到这一点 - PHP i can't save value on textbox into array more than one, i am not yet how to do that 如何使用PHP在选择框中选择从多个选项或具有不同值的数组到视图的设置选项 - How to set an option from multiple options or array with different values to views as selected in select box using PHP xcode11 如何在 PickerView 中为选定的选项提供操作。 这样当我在所选数组中选择一个选项时,我就可以对其执行操作 - xcode11 how to give a selected option an action, in a PickerView. So that when I select an option in the selected array, I can perform an action to it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM