NSFetchrequest.propertiesToFetch中NSRelationship描述的相应返回值类型是什么?

时间:2014-10-22 09:04:11

标签: core-data

我使用NSFetchReuest获取分组数据计数,group on的属性是一个关系属性,我也希望得到返回结果。所以我设置了NSFetchReuest的propertiesToFetch和propertiesToGroupBy。执行结果是一个字典数组。但是我无法获得关系的目标对象。或者我应该如何获得关系的目标对象?代码如下

    let entityDesc = NSEntityDescription.entityForName( "Record" as NSString, inManagedObjectContext: moc)

    let relationDesc:NSRelationshipDescription = entityDesc?.relationshipsByName["relationName" as NSString] as NSRelationshipDescription;

    let keyPathExpression = NSExpression(forKeyPath: "time" as NSString)
    let countExpression = NSExpression(forFunction: "count:", arguments: [keyPathExpression])
    var countExpDescription = NSExpressionDescription()
    countExpDescription.name = "count";
    countExpDescription.expression = countExpression;
    countExpDescription.expressionResultType = NSAttributeType.Integer32AttributeType;


    let fetchRequest = NSFetchRequest(entityName: "Record" as NSString)
    fetchRequest.propertiesToFetch = [relationDesc, countExpDescription];
    fetchRequest.resultType = NSFetchRequestResultType.DictionaryResultType;
    fetchRequest.propertiesToGroupBy = [relationDesc];


    let results = moc.executeFetchRequest(fetchRequest, error: nil)

我在fetchRequest的proertiesToFetch中设置了NSRelationshipDescription,上面的代码可以编译并运行,但是当我尝试获取结果时

  for eachResult in results!
  {
  let eachResultDict = eachResult as [String:AnyObject];

  let relatedObject = eachResultDict["relationName"] ;
  let count =  eachResultDict["count"] ;
  }

我无法确定如何处理relatedObject,它不是目标数据模型的类型...当我尝试打印它时,它只返回一个十六进制数....如何获取相关对象? 代码有点长,我希望我的标题有助于使主题明确〜,谢谢〜

1 个答案:

答案 0 :(得分:1)

relationDesc:NSRelationshipDescription不是NSExpressionDescription

尝试对relationName:NSExpressionDescription进行编码,类似于您对countExpDescription进行编码的方式。