Swift Core Data - Request with distinct results

前端 未结 3 1107
天涯浪人
天涯浪人 2021-01-03 03:08

how I can call es request with distinct values in swift?

This is my code:

let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate a         


        
3条回答
  •  迷失自我
    2021-01-03 03:16

    You need to set

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

    With Swift 2.0 I prefer

    let distinct = NSSet(array: results.map { $0.docID })
    

提交回复
热议问题