Hisham Malik
Hisham Malik

Reputation: 43

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

Upvotes: 4

Views: 4183

Answers (3)

bruno luebke
bruno luebke

Reputation: 71

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

Upvotes: 6

bisamov
bisamov

Reputation: 740

The problem is your entity has one-to-many relationship, if you want to fix this issue try to change it "to one". I know you don't want the answer but here is the hint on how you can fix this. If you go to your *.xcdatamodeld file and locate your Item entity you can see the attributes relationships...find appropriate relationship and modify it from "to-many" to "to-one"

Upvotes: 0

3stud1ant3
3stud1ant3

Reputation: 3606

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?

Upvotes: 2

Related Questions