Swift核心数据-具有不同结果的请求 [英] Swift Core Data - Request with distinct results
本文介绍了Swift核心数据-具有不同结果的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何快速调用具有不同值的es请求?
how I can call es request with distinct values in swift?
这是我的代码:
let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context: NSManagedObjectContext = appDelegate.managedObjectContext
let request = NSFetchRequest(entityName: "MedicalData")
request.propertiesToFetch = NSArray(object: "docID")
request.returnsObjectsAsFaults = false
request.returnsDistinctResults = true
var results:NSArray = context.executeFetchRequest(request, error: nil)
for data in results {
var thisData = data as MedicalData
println(thisData.docID)
}
我想获取"docID"的不同值,但是我得到了所有的实体:(
I want to get distinct values for "docID" but I get all of the entity :(
感谢您的帮助!
推荐答案
您需要设置
request.resultType = NSFetchRequestResultType.DictionaryResultType
它返回字典,但是不同的过滤器应该起作用.
It returns dictionaries, but the distinct filter should work.
如果您不想沿着那条路线走,请在内存中过滤(也建议).进行正常提取,然后
If you do not want to go down that route, filter in memory (also recommended). Do a normal fetch and then
let distinct = NSSet(array: results.valueForKeyPath("docID") as [String])
我更喜欢Swift 2.0
With Swift 2.0 I prefer
let distinct = NSSet(array: results.map { $0.docID })
这篇关于Swift核心数据-具有不同结果的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文