尝试将数据从UIPicker发送到Firebase数据库

时间:2018-09-18 15:45:39

标签: ios swift firebase uipickerview

因此,我想出了如何在将文本字段数据发送到数据库的同时,给每个客户的订单一个唯一的键,在下一个视图或下一个屏幕中,我需要限制客户所做的选择,所以我放置了而不是文本字段具有各种选项的UIPicker。我需要让客户选择的选项也显示在我的数据库中,并且该键与上一个屏幕中使用的键相同。

class flavorViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
    var customerInfo: FIRDatabaseReference!
    @IBOutlet weak var flavorPicker: UIPickerView!

    var flavors: [String] = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
        FIRApp.configure()
        customerInfo = FIRDatabase.database().reference().child("Customer Info");

        self.flavorPicker.dataSource = self
        self.flavorPicker.delegate = self

        //List Picker Data Here
        flavors = ["Double Apple", "Watermelon Mint", "Blueberry Mint", "Grape Mint", "Geisha", "Sex on the Beach"]
    }

    // The number of columns of data
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    // The number of rows of data
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return flavors.count
    }

    // The data to return for the row and component (column) that's being passed in
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return flavors[row]
    }

    func flavorselected() {
        let key = customerInfo.childByAutoId().key

        let flavor = ["id": key,"flavor selected": flavorPicker.selectedRow as Int ]
    }

    @IBAction func checkoutButton(_ sender: Any) {
        flavorselected()
    }

这是我用于设置所有内容的所有代码,但是,我得到一个错误:

  

不能强制将'(Int)-> Int'类型的值转换为'Int'类型

此外,尽管我还没有尝试过,但是此选择是否会与用户在上一个屏幕中输入的数据合并到数据库中?还是会创建一个单独的条目。

1 个答案:

答案 0 :(得分:2)

您的问题在此行上:

/* TEST DATA */
array1 = [
  { name: 'test1', score: 40, date: '2018-09-18T00:00:00.000Z' },
];
array2 = [
  { name: 'test4', score: 50, date: '2018-09-18T00:00:00.000Z' },
  { name: 'test5', score: 40, date: '2018-09-18T00:00:00.000Z' }, 
];
array3 = [
  { name: 'test6', score: 50, date: '2018-09-18T00:00:00.000Z' },
  { name: 'test7', score: 50, date: '2018-09-18T00:00:00.000Z' },
  { name: 'test8', score: 40, date: '2018-09-18T00:00:00.000Z' },
  { name: 'test9', score: 50, date: '2018-09-18T00:00:00.000Z' },
];

testResults = [array1, array2, array3];

// Solution 

function getListOfName(){
  let names = [];
  testResults.map(testResult => {
    testResult.map(({name}) => {if(name) names.push(name)})
  })
  return names;
}
console.log("Full list of names", getListOfName());

// If you want to restrict to K names from each array
function getFirstKNamesfromArray(limit){
  let names = [];
  testResults.map(testResult => {
    testResult.map(({name}, index) => {
      if(name && (index < limit)) names.push(name)
    })
  })
  return names
}
console.log("First 2 names from each array", getFirstKNamesfromArray(2));

原因是:

let flavor = ["id": key,"flavor selected": flavorPicker.selectedRow as Int ]

flavorPicker.selectedRow as Int 是一个带有selectedRow参数的函数。您需要将其更改为:

Int