简体   繁体   中英

Sorting NSFetchedResultsController by Swift Computed Property on NSManagedObjectSubclass

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM