私は日数とタスクを持っています。 1日には多くのタスクがあります。すべてのタスクには「ポイント」という属性があり、現在の日のタスクのすべてのポイントを合計します。私は以下のコードを使用しました(チュートリアルの「コアデータ」のswift 2版にあります)。それをswift 3に変更しようとしました(述語を追加しましたが、それは重要ではありません)。しかし、このコードを実行すると、次のエラーが表示されます。Swift 3コアデータ - NSExpression forFunction: "sum:" throwsエラー( "辞書をDayにキャストできませんでした")
Could not cast value of type 'NSKnownKeysDictionary1' (0x10d02d328) to 'MyProject.Day'
私は間違っていますか?私は明示的に結果はデイ(?私はそれが迅速3の新信じる)になると言う、ここで
let sumRequest: NSFetchRequest<Day> = Day.fetchRequest()
:
// sum current day's task points
let sumRequest: NSFetchRequest<Day> = Day.fetchRequest()
sumRequest.resultType = .dictionaryResultType
sumRequest.predicate = NSPredicate(format: "SELF == %@", argumentArray: [Project.Days.current])
let expressionDescription = NSExpressionDescription()
expressionDescription.name = "sumOfPoints"
expressionDescription.expression = NSExpression(forFunction: "sum:", arguments: [NSExpression(forKeyPath: "tasks.points")])
expressionDescription.expressionResultType = .integer16AttributeType
sumRequest.propertiesToFetch = [expressionDescription]
do {
let results = try managedObject.fetch(sumRequest) as! [NSDictionary] // Here's the line that causes the error
print("DEN:", results)
} catch let error as NSError {
print("DEN:", error.localizedDescription)
}
は、私はそれがこの行とは何かを持っていると思います。しかし、私はこれをどう変えることができるのか分かりません。
ありがとうございました!