[英]Error in CoreData when trying to save an array. 'Cannot convert value of type 'String' to expected argument type 'NSManagedObject''
I am using CoreData to save a tableView to the device. 我正在使用CoreData将tableView保存到设备。 I am relatively new to CoreData, and cannot figure this out. 我对CoreData比较陌生,无法解决这个问题。 I get the error: 我收到错误:
'Cannot convert value of type 'String' to expected argument type 'NSManagedObject''
On the line: 在线上:
favourites.append(addNewMemory.text!)
//MARK:- Core Data
func save(name: String) {
guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return
}
// 1
let managedContext =
appDelegate.persistentContainer.viewContext
// 2
let entity =
NSEntityDescription.entity(forEntityName: "Memory",
in: managedContext)!
let person = NSManagedObject(entity: entity,
insertInto: managedContext)
// 3
person.setValue(name, forKeyPath: "name")
// 4
do {
try managedContext.save()
favourites.append(person)
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
}
var favourites: [NSManagedObject] = []
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return favourites.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "Cell")
/*cell.imageView?.image = UIImage(named: "applelogo")
cell.imageView?.setRounded()
cell.imageView?.clipsToBounds = true
*/
let favMemory = favourites[indexPath.row]
cell.textLabel?.text = favMemory.value(forKeyPath: "name") as? String
return cell
}
@IBAction func addButtonTapped(_ sender: UIButton) {
insertNewCell()
}
func insertNewCell() {
favourites.append(addNewMemory.text!)
let indexPath = IndexPath(row: favourites.count - 1, section: 0)
tableView.beginUpdates()
tableView.insertRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
addNewMemory.text = ""
view.endEditing(true)
}
I expected the app to save the string, but it does not work. 我希望应用程序保存字符串,但它不起作用。 How can I fix this? 我怎样才能解决这个问题?
You are mixing up the text
property of the text field and the Core Data entity. 您正在混合文本字段的text
属性和核心数据实体。 Obviously favourites
is declared as [NSManagedObject]
so you can't append a string. 显然, favourites
被声明为[NSManagedObject]
因此您无法附加字符串。 That's what the error message is telling you. 这就是错误消息告诉你的内容。
You have to insert a new record in insertNewCell
. 您必须在insertNewCell
插入新记录。 The easiest solution is to call save
and return a Bool from save
to indicate that the insertion was successful. 最简单的解决方案是调用save
并从save
返回Bool以指示插入成功。
And you are encouraged to use more contemporary API. 并鼓励您使用更现代的API。 If there is no Memory
subclass create one 如果没有Memory
子类创建一个
var favourites = [Memory]()
...
func save(name: String) -> Bool {
let appDelegate = UIApplication.shared.delegate as! AppDelegate // force unwrapping is perfectly fine
// 1
let managedContext = appDelegate.persistentContainer.viewContext
// 2
let person = Memory(context: managedContext)
// 3
person.name = name
// 4
do {
try managedContext.save()
favourites.append(person)
return true
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
return false
}
}
and change insertNewCell
to 并将insertNewCell
更改为
func insertNewCell() {
guard save(name: addNewMemory.text!) else { return }
let indexPath = IndexPath(row: favourites.count - 1, section: 0)
tableView.insertRows(at: [indexPath], with: .automatic)
addNewMemory.text = ""
view.endEditing(true)
}
beginUpdates/endUpdates
is pointless. beginUpdates/endUpdates
是没有意义的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.