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
    }
8
  • I'm not familiar with doctor, So not sure about that part. Have to tried to print the objects returned from parse? what is the actual issue that your having? are the values not being returned? or they are returned but not showing in the picker view? Commented Oct 10, 2015 at 12:36
  • doctors is parse table name, doctor is the name of textfield that takes the value of viewPicker. My main problem is values not being returned. I need to take these values. Commented Oct 10, 2015 at 12:41
  • What does your log display ? Commented Oct 10, 2015 at 12:51
  • My log says: fatal error: Array index out of range Commented Oct 10, 2015 at 13:06
  • You need to provide some additional info to get help with this issue, can you provide the values passed to objects and error? is there any suggestion of what array is out of bounds? how is the array defined? Commented Oct 10, 2015 at 13:14

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

How do I sort these values alphabetically before printing to screen? Is there any way?
you should be able to add a sort property on the query in parse, otherwise you'd need to use the global sort function provided by swift.
query.orderByAscending("doctorNames") is the solution :} Thanks a lot again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.