У меня есть такая модель данных:
Я хочу получить список сумм по группам по catalog.item, money.year, money.month, и япытался сделать это несколькими способами, которые можно найти в Интернете, но мне не удалось.
Я попытался "let fetchRequest = NSFetchRequest (entityName:" CashFlow ")"и "let fetchRequest = NSFetchRequest (entityName:" CashFlow ")"
Это всегда одна и та же ошибка: печатается описание fetchRequest: выражение вызвало ошибку: ошибка: /var/folders/8j/g0yb8vwx23z2s1qykxz2q7hh0000gn/T/expr51-6487f6..swift:1:65: ошибка: использованиенеобъявленный тип 'CoreData' Swift._DebuggerSupport.stringForPrintObject (Swift.UnsafePointer> (bitPattern: 0x127497460) !. pointee)
class MonthlySum {
public var year:Int16 = 0
public var month:Int16 = 0
public var emoji:String = ""
public var item:String = ""
public var budget:Int16 = 0
public var total:Int16 = 0
}
// Я также пытался определить MonthlySum как NSManagedObject
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName:"CashFlow")
// Set the batch size to a suitable number.
//fetchRequest.fetchBatchSize = 32
fetchRequest.predicate = NSPredicate(format: "money.year = %d and money.month = %d",year,month)
// sort by day
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "money.cata.item", ascending: true)]
//group by day for sum
//fetchRequest.propertiesToGroupBy = ["money.cata.emoji","money.cata.item","money.budget"]
var expressionDescriptions = [AnyObject]()
var expD = NSExpressionDescription()
// select year
expD.name = "year"
expD.expression = NSExpression(forKeyPath: "money.year")
expD.expressionResultType = .integer16AttributeType
expressionDescriptions.append(expD)
// select month
expD.name = "month"
expD.expression = NSExpression(forKeyPath: "money.month")
expD.expressionResultType = .integer16AttributeType
expressionDescriptions.append(expD)
// select category item
expD.name = "item"
expD.expression = NSExpression(forKeyPath: "money.cata.item")
expD.expressionResultType = .stringAttributeType
expressionDescriptions.append(expD)
// select category emoji
expD.name = "emoji"
expD.expression = NSExpression(forKeyPath: "money.cata.emoji")
expD.expressionResultType = .stringAttributeType
expressionDescriptions.append(expD)
// select month
expD.name = "budget"
expD.expression = NSExpression(forKeyPath: "money.budget")
expD.expressionResultType = .integer16AttributeType
expressionDescriptions.append(expD)
//select @sum.amount as dailysum
expD = NSExpressionDescription()
expD.name = "total"
expD.expression = NSExpression(forFunction: "sum:", arguments: [NSExpression(forKeyPath: "amount")])
expD.expressionResultType = .integer16AttributeType
expressionDescriptions.append(expD)
fetchRequest.propertiesToFetch = expressionDescriptions
fetchRequest.resultType = .dictionaryResultType
var result = [MonthlySum]()
let m = MonthlySum()
do {
let fetchResult = try viewContext.fetch(fetchRequest) as! [MonthlySum]
/*
for item in fetchResult {
m.budget = item.value(forKey: "budget") as! Int16
m.emoji = item.value(forKey: "emoji") as! String
m.item = item.value(forKey: "item") as! String
m.month = item.value(forKey: "month") as! Int16
m.total = item.value(forKey: "total") as! Int16
m.year = item.value(forKey: "year") as! Int16
result.append(m)
} */
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}