Swift:如何过滤核心数据 [英] Swift: How to filter in Core Data
本文介绍了Swift:如何过滤核心数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码来提取类别中的所有数据。
I'm using the following code to fetch all the data in "category".
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let managedContext = appDelegate.managedObjectContext!
let fetchRequest = NSFetchRequest(entityName:"category")
let fetchedResults = managedContext.executeFetchRequest(fetchRequest, error: &error) as [NSManagedObject]?
我如何只带类型等于产品的类别?
How do I only brings categories where the "type" is equal to "products"?
推荐答案
要过滤 Core Data中的结果,请使用 NSPredicate
,如下所示:
To "filter" results in Core Data, use NSPredicate
like so:
let filter = "products"
let predicate = NSPredicate(format: "type = %@", filter)
fetchRequest.predicate = predicate
这篇关于Swift:如何过滤核心数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文