Berk Kaya
Berk Kaya

Reputation: 470

Retrieving data from Parse using Swift 2.0

I need a bit help, I've made a pickerview and i want to retrieve it's data from Parse.

I need to assign values to array and show it on Picker View.

Help me guys. Thanks.

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()

                }

            }


        })
}

And Here my pickerView Functions:

  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
    }

Upvotes: 0

Views: 474

Answers (1)

Scriptable
Scriptable

Reputation: 19758

NOTE: I cannot post well formatted code in the comments, so I'll put this here for now. If I manage to solve the issue then I'll leave it here with edits, if not I'll delete it shortly.

It's possible that the query is not returning anything or doctorNames is not a valid property on the returned object. try this code and let us know the result

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)")
}

EDIT:

After the op posting log results, the error was in the filter. adding query.whereKey("doctorId", equalTo:"1") did not return any results. Omitting this line will return all results though.

Upvotes: 1

Related Questions