Swift Core Data - 组不起作用 [英] Swift Core Data - Group doesn't Work

查看:69
本文介绍了Swift Core Data - 组不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的 iOS Swift 应用程序,但找不到任何解决核心数据小问题的方法.我必须对从核心数据中提取的一些数据进行分组,但它不起作用.

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.

这是我的 fetchRequest:

This is my fetchRequest:

<NSFetchRequest: 0x7f82fbe105e0> (entity: Sets; predicate: ((null)); sortDescriptors: ((
    "(sort, ascending, compare:)"
)); type: NSDictionaryResultType; includesPendingChanges: NO; propertiesToFetch: ((
    name
)); propertiesToGroupBy: ((
    name
)); )

我已经排序并添加了要分组的属性.还设置了一个结果类型——DictionaryResultType.

I have sort and added properties to group by. There is also set a result type - DictionaryResultType.

这是我的 FetchRequest:

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]

  • 实体是一个字符串变量
  • groupDescriptors 和 propertiesToFetch 是字符串数组
  • 推荐答案

    好的,我找到了一个解决方案,而且很简单.

    Ok I found a solution and it's just simple.

    managedContext.executeFetchRequest(fetchRequest as NSFetchRequest, error: &error) as? [NSManagedObject]
    

    只生成 NSManagedObject 的.如果您将获取请求更改为

    generates only NSManagedObject's. If you change the fetch request to

    managedContext.executeRequest(fetchRequest, withContext: managedContext as! NSManagedObjectContext, error: nil) as! [AnyObject]
    

    您将收到您的数据.

    这篇关于Swift Core Data - 组不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆