Swift Core Data Predicate IN Clause

青春壹個敷衍的年華 提交于 2019-12-21 14:09:32

问题


I'm attempting to use an IN clause with an NSPredicate. I'm getting the following error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0xa000000000000611'

Here's the code:

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()

Any ideas as to what the problem is?


回答1:


You don't show how you are defining recentEmployeeIds, but assuming its something like

let recentEmployeeIds:[Int] = ...

then you need to init the NSPredicate correctly. There is no label for argumentArray

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


来源:https://stackoverflow.com/questions/42652555/swift-core-data-predicate-in-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!