我有带值的int数组
例子:
var selectedcard=[int]()
[3,1,2]
我需要按顺序编辑所有记录
我试过但应用程序崩溃了
swift - 按ID编辑所有记录(Swift 4,CoreData)-LMLPHP

for item in 0...SelectedCard.count-1 {
        let order = SelectedCard[item]

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

        let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Card")
        fetchRequest.predicate = NSPredicate(format: "order == %@", order)

        do {
            if let fetchResults = try managedContext.fetch(fetchRequest) as? [Card] {
                if fetchResults.count != 0 {
                    for index in 0...fetchResults.count-1 {
                        let managedObject = fetchResults[index]
                        managedObject.setValue("", forKey: "folderID")
                    }
                    appDelegate.saveContext()
                }
            }
        } catch {
            print(error)
        }
}
self.tableView.reloadData()

最佳答案

尝试将谓词更改为

NSPredicate(format: "order == \(order)")

关于swift - 按ID编辑所有记录(Swift 4,CoreData),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56064625/

10-11 06:41