我用一個NSFetchRequest獲取分組數據,要分組的屬性是一個關係屬性,我也希望獲得返回結果。所以我設置NSFetchRequest的propertiesToFetch和propertiesToGroupBy。執行結果是一個字典數組。但我無法獲得關係的目標對象。或者我應該如何獲得關係的目標對象?代碼如下NSFetchrRequst.propertiesToFetch中的NSRelationshipDescription的cooresponding返回值類型是什麼?
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做,它不是目標數據模型的類型......當我嘗試打印它時,它只是返回一個十六進制數字....如何獲取相關對象? 的代碼是有點長,我希望我的標題有助於使主題明確〜,謝謝〜