Swift 3 Core Data - NSExpression forFunction:“sum:”抛出错误(“无法将字典转换为日”) [英] Swift 3 Core Data - NSExpression forFunction: "sum:" throws error ("could not cast dictionary to Day")

查看:320
本文介绍了Swift 3 Core Data - NSExpression forFunction:“sum:”抛出错误(“无法将字典转换为日”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有天和任务。一天有很多任务。每个任务都有一个称为点的属性,我想对当天任务的所有点求和。我使用下面的代码(在Core Data中通过教程,swift 2版本找到),并试图修改它为swift 3(我也添加了一个谓词,但这不重要)。但是当我运行这个代码,我得到这个错误:


无法将值'NSKnownKeysDictionary1'(0x10d02d328)转换为'MyProject。日


我做错了什么?

 code> // 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] //这是导致错误的行
print(DEN:,results)
} catch let错误为NSError {
print(DEN:,error.localizedDescription )
}

我认为它与这一行有关:

  let sumRequest:NSFetchRequest< Day> = Day.fetchRequest()

在这里,我明确地说结果将是一天new in swift 3?)。

解决方案



这解决了我的问题:

  let sumRequest:NSFetchRequest< NSFetchRequestResult> = Day.fetchRequest()

而不是 Day 作为 NSFetchRequestResult ,我把 NSFetchRequestResult 本身。



我认为我将它设置为不同的 resultType 它不是 [Day] 不再返回,所以这就是为什么我需要更改为更通用或某事。


I have days and tasks. A day has many tasks. Every task has an attribute called "points" and I want to sum all points of the tasks of the current day. I used the code below (found in the book Core Data by tutorials, swift 2 version) and tried to modify it for swift 3 (I also added a predicate, but that's not important). But when I run this code, I get this error:

Could not cast value of type 'NSKnownKeysDictionary1' (0x10d02d328) to 'MyProject.Day'

What am I doing wrong?

    // 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)
    }

I think it has something to do with this line:

let sumRequest: NSFetchRequest<Day> = Day.fetchRequest()

Here, I explicitly say the result will be a Day (I believe that's new in swift 3?). But I don't know how I could change this.

Thank you!

解决方案

This solves my problem:

let sumRequest: NSFetchRequest<NSFetchRequestResult> = Day.fetchRequest()

Instead of putting Dayas NSFetchRequestResult, I put NSFetchRequestResult itself there. Now it's working.

I believe as I set it to a different resultType it's not [Day]anymore that gets returned, so that's why I needed to change that to be more generic or something.

这篇关于Swift 3 Core Data - NSExpression forFunction:“sum:”抛出错误(“无法将字典转换为日”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆