How to save selected pickerView value to CoreData

Hello all:


I am looking for a way to save a pickerView selection to CoreData with no success so far. I want to save the  row numerical value from 0 – 6, depending on the selected value. Below is the code I am trying but it is not working. It is saving random values not the one selected:


  @IBAction func saveRating(_ sender: Any) {        

            guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {



            let managedContext = appDelegate.persistentContainer.viewContext

            let entity = NSEntityDescription.entity(forEntityName: “DecisionComponents”, in: managedContext)!

            let component = NSManagedObject(entity: entity, insertInto: managedContext)


            let whyNowTitles = [“very poor reason”,”poor reason”,”passable reason”, “good reason” , “very good reason”, “excellent reason”]


        func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

            selectedRating = whyNowTitles[row]

            textLabel.text = selectedRating

            let stored = row

            print (stored)



             for (stored) in whyNowTitles {

             component.setValue(stored.self, forKeyPath: “components”)       

            do {



                print (“SAVED”)

            } catch let error as NSError {

                print(“Could not save. (error), (error.userInfo)”)






Powered by WPeMatico

You may also like...

Comments are closed.