我有一個tableView,並且該tableview正在填充來自Coredata的數據。數據如此崩潰。在實體內爲CoreData添加屬性ios 10
實體 - 人
實體 - 聲明 聲明實體呼叫amountOwed一個屬性,它是小數類型。
關係是一個人可以有很多陳述,但每個陳述屬於一個人。
這裏是我想加起來的數據的路徑。 Person> Statement> AmountOwed。
在tableView函數中,我有一個表示Person實體的let。
let person = fetchedResultsController.object(at: indexPath)
我知道它的工作,因爲我可以打印出的人的名字,像這樣
print(person.name) // Bob
我希望能夠做的就是添加了所有的amountOwed的聲明實體內的每個人的屬性是什麼並將它們顯示在單元格上。
我一直在試圖追蹤計算提取的例子,但我似乎並不清楚如何定位鏈接到每個Person實體的我的Statements實體。
let fetchRequest = NSFetchRequest<NSDictionary>(entityName:"statement")
fetchRequest.resultType = .dictionaryResultType
let sumExpressionDesc = NSExpressionDescription()
sumExpressionDesc.name = "sumDeals"
let specialCountExp = NSExpression(forKeyPath: #keyPath(Person.statement[indexPath].amountOwed))
sumExpressionDesc.expression = NSExpression(forFunction: "sum:", arguments: [specialCountExp])
sumExpressionDesc.expressionResultsType = .interger32AttributeType
fetchRequest.propertiesToFetch = [sumExpressionDesc]
do{
let results = try coreDataStack.managedContext.fetch(fetchRequest)
let resultDict = results.first!
let numDeals = resultDict["sumDeals"]
print(numDeals!)
}
catch let error as NSError{
print("Count not fetched \(error), \(error.userInfo)")
}
我需要獲取一個Statement實體還是應該使用FetchedREsultsController?如果我使用我的fetchedResultsController做了的keyPath的聲明實體這個樣子
person[indexPath].statement.amountOwed
哇,多麼偉大的解決方案,如此簡單和乾淨。謝謝! – icekomo