請幫我:從SWIFT核心數據刪除的UITableView的選擇的數據3
PersonModel.swift
class PersonModel: NSObject {
dynamic var name: String?
override init() {
}
init(person: Person) {
self.name = person.name
}
}
在ViewController.swift文件我已經創建了一個數組「變種peopleList = PersonModel「uiang保存並獲取函數我已保存並提取了我的所有數據
以下部分用於從表和核心da中刪除特定的選定行數據ta在同一時間。
// MARK: - UITableViewDataSource
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
// let deletePerson = PersonModel()
if editingStyle == .delete {
let deletePersonData = peopleList[indexPath.row]
PersonServices.sharedInstance.deletePerson(deletePersonData)
peopleList.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}
tableView.reloadData()
}
PersonServices.swift
class PersonServices: BusinessClass {
static var sharedInstance = PersonServices()
func deletePerson(deletePersonData)-> [PersonModel] {
var personModels = [PersonModel]();
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: PersonEntity)
// let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: PersonEntity)
let predicates = NSPredicate(format: "name == %@", "PeopleList[index]")
fetchRequest.predicate = predicates
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest as! NSFetchRequest<NSFetchRequestResult>)
do {
try managedObjectContext.execute(deleteRequest)
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
}
,但我不能這樣做that.please幫助我..
是你的問題解決了嗎? – 3stud1ant3
是的,解決了這個問題。見下圖。 – salma209