Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xcode cannot assign value of type.. to NSSet

I am working on CoreData with Swift 3 and I am almost done. I did the CoreData entities and now I am trying to save data from a form on the app and I got this error:

  import UIKit
  import CoreData

  class ItemDetailsVC: UIViewController, UIPickerViewDelegate,          UIPickerViewDataSource  {

@IBOutlet weak var storePicker: UIPickerView!
@IBOutlet weak var titleField: CustomTextField!
@IBOutlet weak var priceField: CustomTextField!
@IBOutlet weak var detailsField: CustomTextField!

var stores = [Store]()


override func viewDidLoad() {
    super.viewDidLoad()

    storePicker.delegate = self
    storePicker.dataSource = self



func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

    let store = stores[row]
    return store.name
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {

    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

    return stores.count
}

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

    // update
}

func getStores() {
    let fetchRequest: NSFetchRequest<Store> = Store.fetchRequest()

    do {
        self.stores = try context.fetch(fetchRequest)
        self.storePicker.reloadAllComponents()

    } catch {
        // some code
    }

}

@IBAction func savePressed(_ sender: UIButton) {

    let item = Item(context: context)

    if let title = titleField.text {
        item.title = title
    }

    if let price = priceField.text {
        item.price = (price as NSString).doubleValue
    }

    if let details = detailsField.text {
        item.details = details
    }

   item.toStore = stores[storePicker.selectedRow(inComponent: 0)] error:    cannot assign value of type 'Store' to value of type NSSet?

}

I am not asking anyone to solve the error, I just wanna know what the error is telling me to do. Error is on the item.toStore = stores[storePicker... line

like image 521
Hisham Malik Avatar asked Oct 22 '25 04:10

Hisham Malik


2 Answers

When you set a relationship on CoreData model to To Many it starts behaving like an Array. And like so you need to append the data to that array instead of assigning an class to it. You can do so with:

.addingObjects(from: [Any])
like image 79
bruno luebke Avatar answered Oct 25 '25 06:10

bruno luebke


I am not asking anyone to solve the error, I just wanna know what the error is telling me to do.

From what I have understood is that toStore is a to many relationship from Item to Store entity in CoreData.

Now since you need to know about the error only ,if I am correct and the relationship is exactly above then it means that Item entity has a set of stores associated with it not a single Store , hence you are getting this error

error: cannot assign value of type 'Store' to value of type NSSet?

like image 23
3stud1ant3 Avatar answered Oct 25 '25 06:10

3stud1ant3



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!