使用Swift 2.0从Parse检索数据

时间:2015-10-10 12:29:53

标签: ios arrays swift parse-platform

我需要一点帮助,我做了一个pickerview,我想从Parse中检索它的数据。

我需要为数组赋值并在Picker View上显示它。

帮助我们。 感谢。

override func viewDidLoad() {
super.viewDidLoad()


        let pickerView = UIPickerView()
        pickerView.delegate = self
        doctor.inputView = pickerView

        let query = PFQuery(className: "doctors")
        //wherekey can be omited
        query.whereKey("doctorId", equalTo:"1")

        query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

            if let objects = objects {

                for object in objects {

                    self.doctorNames.append(object["doctorNames"] as! String)

                    pickerView.reloadAllComponents()

                }

            }


        })
}

这里我的pickerView功能:

  func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return doctorNames.count
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return doctorNames[row]
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        doctor.text = doctorNames[row]  //bad inscturctions error on this line
    }

1 个答案:

答案 0 :(得分:1)

注意:我无法在评论中发布格式良好的代码,所以我现在就把它放在这里。如果我设法解决问题,那么我会将其留在这里进行编辑,如果不是,我会很快将其删除。

查询可能没有返回任何内容,或者doctorNames不是返回对象的有效属性。试试这段代码,让我们知道结果

if let objects = objects {
    print("objects returned: \(objects)")
    for object in objects {
        print("adding object to pickerView: \(object)")
        self.doctorNames.append(object["doctorNames"] as! String)
    }
    // also I'd put this outside of the loop
    pickerView.reloadAllComponents()
} else {
    print("something went wrong here. Objects is: \(objects), error is: \(error)")
}

编辑:

在op发布日志结果后,错误发生在过滤器中。添加query.whereKey("doctorId", equalTo:"1")未返回任何结果。省略此行将返回所有结果。