Я пытаюсь использовать функцию, найденную на этом сайте, связанную с удалением из основных данных с помощью функции смахивания из таблицы, но я не могу понять, что от меня требует это сообщение об ошибке?Может кто-нибудь помочь с этой ошибкой, пожалуйста?
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()
}
}
}