本文介绍了Swift核心数据谓词IN子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将IN子句与NSPredicate一起使用.我收到以下错误:

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

代码如下:

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?

推荐答案

您没有展示如何定义recentEmployeeIds,而是假设其类似

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

let recentEmployeeIds:[Int] = ...

然后您需要正确地初始化NSPredicate. argumentArray

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

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

这篇关于Swift核心数据谓词IN子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 12:47