问题
I am working on a simple iOS Swift app and can't find any solution for a small problem with core data. I have to group some data which i fetch out of core data, but it doesn't work.
This is my fetchRequest:
<NSFetchRequest: 0x7f82fbe105e0> (entity: Sets; predicate: ((null)); sortDescriptors: ((
"(sort, ascending, compare:)"
)); type: NSDictionaryResultType; includesPendingChanges: NO; propertiesToFetch: ((
name
)); propertiesToGroupBy: ((
name
)); )
I have sort and added properties to group by. There is also set a result type - DictionaryResultType.
And here is my FetchRequest:
fetchRequest = NSFetchRequest(entityName:entity)
fetchRequest.propertiesToGroupBy = groupDescriptors
fetchRequest.propertiesToFetch = propertiesToFetch
fetchRequest.resultType = .DictionaryResultType
let fetchedResults = managedContext.executeFetchRequest(fetchRequest as NSFetchRequest, error: &error) as? [NSManagedObject]
- Entity is a string variabel
- groupDescriptors and propertiesToFetch are arrays of String
回答1:
Ok I found a solution and it's just simple.
managedContext.executeFetchRequest(fetchRequest as NSFetchRequest, error: &error) as? [NSManagedObject]
generates only NSManagedObject's. If you change the fetch request to
managedContext.executeRequest(fetchRequest, withContext: managedContext as! NSManagedObjectContext, error: nil) as! [AnyObject]
you will receive your data.
来源:https://stackoverflow.com/questions/30473737/swift-core-data-group-doesnt-work