gpt4 book ai didi

swift - xcode 无法将 type.. 的值分配给 NSSet

转载 作者:行者123 更新时间:2023-11-28 08:00:00 34 4
gpt4 key购买 nike

我正在使用 Swift 3 处理 CoreData,我快完成了。我做了 CoreData 实体,现在我试图从应用程序的表单中保存数据,但出现此错误:

  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?

}

我不要求任何人解决错误,我只是想知道错误告诉我做什么。错误在 item.toStore = stores[storePicker... 行上

最佳答案

当您将 CoreData 模型上的关系设置为 To Many 时,它开始表现得像一个数组。就像这样,您需要将数据附加到该数组,而不是为其分配一个类。您可以这样做:

.addingObjects(from: [Any])

关于swift - xcode 无法将 type.. 的值分配给 NSSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46922711/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com