在NSManagedObjectSubclass上通过Swift计算属性对NSFetchedResultsController进行排序 [英] Sorting NSFetchedResultsController by Swift Computed Property on NSManagedObjectSubclass

查看:330
本文介绍了在NSManagedObjectSubclass上通过Swift计算属性对NSFetchedResultsController进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Swift和Core Data构建一个应用程序。在我的应用程序的某一点,我想有一个 UITableView 显示当前在持久存储中的类型的所有对象。目前,我正在检索它们,并使用 NSFetchedResultsController 在表视图中显示它们。我想通过我的 NSManagedObject 子类的计算属性对表视图进行排序,如下所示:

  class MHClub:NSManagedObject {
@NSManaged var name:String
@NSManaged var shots:NSSet
var averageDistance:Int {
get {
if shots.count> 0 {
var total = 0
拍摄镜头{
total + =(拍摄为!MHShot).distance.integerValue
}
返回总计/拍摄。 count
}
else {
return 0
}
}
}

在我的表视图控制器中,我设置了 NSFetchedResultsController fetchRequest 如下:

  let request = NSFetchRequest(entityName:MHClub)
request.sortDescriptors = [ NSSortDescriptor(key:averageDistance,ascending:true),NSSortDescriptor(key:name,ascending:true)]


这样设置会导致我的应用程序崩溃,在日志中出现以下消息:

 'NSInvalidArgumentException' ,reason:'keypath averageDistance not found in entity< NSSQLEntity MHClub id = 1>'

取出第一个排序描述符,我的应用程序运行正常,但我的表视图没有排序我想要它是如何。如何根据Swift中 NSManagedObject 子类的计算属性排序表视图?

解决方案

正如Martin所指出的,您不能对计算属性进行排序。只需更新俱乐部的每一次拍摄的新存储的属性。


I'm building an app using Swift with Core Data. At one point in my app, I want to have a UITableView show all the objects of a type currently in the Persistent Store. Currently, I'm retrieving them and showing them in the table view using an NSFetchedResultsController. I want the table view to be sorted by a computed property of my NSManagedObject subclass, which looks like this:

class MHClub: NSManagedObject{
@NSManaged var name: String
@NSManaged var shots: NSSet
var averageDistance: Int{
    get{
        if shots.count > 0{
            var total = 0
            for shot in shots{
                total += (shot as! MHShot).distance.integerValue
            }
            return total / shots.count
        }
        else{
            return 0
        }
    }
}

In my table view controller, I am setting up my NSFetchedResultsController's fetchRequest as follows:

let request = NSFetchRequest(entityName: "MHClub")
request.sortDescriptors = [NSSortDescriptor(key: "averageDistance", ascending: true), NSSortDescriptor(key: "name", ascending: true)]

Setting it up like this causes my app to crash with the following message in the log:

'NSInvalidArgumentException', reason: 'keypath averageDistance not found in entity <NSSQLEntity MHClub id=1>'

When I take out the first sort descriptor, my app runs just fine, but my table view isn't sorted exactly how I want it to be. How can I sort my table view based on a computed property of an NSManagedObject subclass in Swift?

解决方案

As was pointed out by Martin, you cannot sort on a computed property. Just update a new stored property of the club every time a shot is taken.

这篇关于在NSManagedObjectSubclass上通过Swift计算属性对NSFetchedResultsController进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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