Ios 尝试按一个属性分组,并获取核心数据中每个组中的实体数

Ios 尝试按一个属性分组,并获取核心数据中每个组中的实体数,ios,swift,core-data,count,group-by,Ios,Swift,Core Data,Count,Group By,我有一个实体包。我想按myStore筛选,按myShoppingItem分组,并获取每个组的计数 我的提取请求编码如下: let historyRequest = NSFetchRequest(entityName: "PackageHistories") let storePredicate = NSComparisonPredicate(leftExpression: NSExpression(forKeyPath: "myStore"), rightExpr

我有一个实体包。我想按myStore筛选,按myShoppingItem分组,并获取每个组的计数

我的提取请求编码如下:

        let historyRequest = NSFetchRequest(entityName: "PackageHistories")
        let storePredicate = NSComparisonPredicate(leftExpression: NSExpression(forKeyPath: "myStore"), rightExpression: NSExpression(forConstantValue: dataStack.currentReceipt.myStore), modifier: NSComparisonPredicateModifier.DirectPredicateModifier, type: NSPredicateOperatorType.EqualToPredicateOperatorType, options: NSComparisonPredicateOptions.NormalizedPredicateOption)
        historyRequest.predicate = storePredicate
        historyRequest.resultType = NSFetchRequestResultType.DictionaryResultType

        let itemDescription = NSExpressionDescription()
        itemDescription.name = "ShoppingItems"
        itemDescription.expression = NSExpression(forKeyPath: "myShoppingItem")
        itemDescription.expressionResultType = .ObjectIDAttributeType

        let countDescription = NSExpressionDescription()
        countDescription.name = "Popularity"
        countDescription.expression = NSExpression(format: "@count.myShoppingItem")
        countDescription.expressionResultType = .Integer32AttributeType

        historyRequest.propertiesToGroupBy = ["myShoppingItem"]
        historyRequest.propertiesToFetch = [itemDescription, countDescription]
        historyRequest.sortDescriptors = [NSSortDescriptor(key: "Popularity", ascending: false)]
我的问题是,我遇到了一个致命错误:
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因是:“无法为表达式@count.myShoppingItem'
生成选择目标令牌。”

在最近的几个版本中是否发生了更改,以致
countDescription.expression=NSExpression(格式:“@count.myShoppingItem”)
不再是正确的格式?我使用的是Xcode 7.2和iOS 9

更新:

countDescription.expression = NSExpression(forFunction: "count:", arguments: [itemDescription.expression!])
countDescription.expressionResultType = .Integer32AttributeType

没有崩溃,但它确实让我狼吞虎咽。它不返回整数,而是返回某种NSManagedObjectID。

您的代码看起来正常。但它不应该是myShoppingItem。@count