gpt4 book ai didi

swift - 无法将类型 '[NSManagedObject]' 的值转换为预期参数类型 'NSManagedObject'

转载 作者:行者123 更新时间:2023-11-30 11:30:26 25 4
gpt4 key购买 nike

我正在尝试使用我在该网站上找到的一个与使用 TableView 中的滑动功能从核心数据中删除相关的功能,但我无法弄清楚此错误消息需要我做什么?有人可以帮忙解决这个错误吗?

import UIKit
import CoreData

class ViewController: UIViewController, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var selectRandom: UIButton!
@IBOutlet weak var removeName: UIButton!



var people: [NSManagedObject] = []

override func viewDidLoad() {
super.viewDidLoad()

title = "Spiller"
tableView.register(UITableViewCell.self,
forCellReuseIdentifier: "Cell")
self.tableView.dataSource = self
self.tableView.delegate = self


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

let managedContext = appDelegate.persistentContainer.viewContext

let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Player")
do {
people = try managedContext.fetch(fetchRequest)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
}


@IBAction func addName(_ sender: UIBarButtonItem) {


let alert = UIAlertController(title: "Nytt navn",
message: "Legg til ny spiller",
preferredStyle: .alert)

let saveAction = UIAlertAction(title: "Lagre", style: .default) { [unowned self] action in

guard let textField = alert.textFields?.first,
let nameToSave = textField.text else {
return
}

self.save(name: nameToSave)
self.tableView.reloadData()
}

let cancelAction = UIAlertAction(title: "Avbryt",
style: .default)

alert.addTextField()

alert.addAction(saveAction)
alert.addAction(cancelAction)

present(alert, animated: true)
}

func save(name: String) {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}

let managedContext = appDelegate.persistentContainer.viewContext

let entity = NSEntityDescription.entity(forEntityName: "Player",
in: managedContext)!

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

Player.setValue(name, forKeyPath: "name")

do {
try managedContext.save()
people.append(Player)
} catch let error as NSError {
print("Kunne ikke lagre. \(error), \(error.userInfo)")
}

}
@IBAction func randomPlayer(_ sender: UIButton) {


}
@IBAction func extraGame(_ sender: UIButton) {
}
@IBAction func walkOver(_ sender: UIButton) {
}
@IBAction func trash(_ sender: UIBarButtonItem) {
if removeName.isEnabled{


}

}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()


}
}


// MARK: - UITableViewDataSource
extension ViewController: UITableViewDataSource {

func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return people.count
}

func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let Player = people[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell",
for: indexPath)
cell.textLabel?.text = Player.value(forKeyPath: "name") as? String
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
people.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

let noteEntity = "Player" //Entity Name

let managedContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

let note = people[indexPath.row]

if editingStyle == .delete {
managedContext.delete(people)

do {
try managedContext.save()
} catch let error as NSError {
print("Error While Deleting Note: \(error.userInfo)")
}

}

//Code to Fetch New Data From The DB and Reload Table.
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: noteEntity)

do {
people = try managedContext.fetch(fetchRequest) as! [Player]
} catch let error as NSError {
print("Error While Fetching Data From DB: \(error.userInfo)")
}
self.tableView.reloadData()
}



}
}

最佳答案

问题就在这里

if editingStyle == .delete {
managedContext.delete(people)

as managedContext.delete 采用 NSManagedObject 类型的参数,当前您传递的 people[NSManagedObject]

您必须删除注释

managedContext.delete(note) 

关于swift - 无法将类型 '[NSManagedObject]' 的值转换为预期参数类型 'NSManagedObject',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50319802/

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