Swift Core数据谓词子句中的谓词



我正在尝试使用nspredicate的子句。我会收到以下错误:

***终止由于未知的例外" nsinvalidargumentException"终止应用程序,原因是:' - [nstaggedPointerString countbyenumeratingwithState:obsocts:count:]:未识别的选择器发送到实例0xA00000000000000611'

' '

这是代码:

let fetchRequest: NSFetchRequest<Employee> = NSFetchRequest(entityName: "Employee")
fetchRequest.sortDescriptors = [
     NSSortDescriptor.init(key: "lastName", ascending: true)
]
fetchRequest.predicate = NSPredicate(format: "ANY id IN %@", argumentArray: recentEmployeeIds)
fetchedResultsController = NSFetchedResultsController.init(fetchRequest: fetchRequest,
                                                                           managedObjectContext: FLCoreDataController.shared.mainObjectContext,
                                                                           sectionNameKeyPath: nil,
                                                                           cacheName: nil)
fetchedResultsController?.delegate = self
try? fetchedResultsController?.performFetch()

关于问题是什么的想法?

您不显示您如何定义 recentEmployeeIds,而是假设它像

let recentEmployeeIds:[Int] = ...

然后,您需要正确启动NSPredicateargumentArray

没有标签
fetchRequest.predicate = NSPredicate(format: "ANY id IN %@", recentEmployeeIds)

您不能再删除```grign array''(在Swift 5.7中测试)

Let duplicates = [“id”, “id2”]
let findDuplicates = <#NSManagedObject#>.fetchRequest()
findDuplicates.predicate = NSPredicate(format: <#key#> IN %@", argumentArray: [duplicates])

区别在于,您将数组放在数组中,因此将其插入为第一个项目。您只有一个%@,因此数组中只有1个参数,但是当您通过"重复"时,有2个项目会出现错误。将其包裹在数组中可以解决。

最新更新