使用NSFetchedResultsController对排序描述符进行分类 - Swift

6
我有一个从Core Data获取数据的UITableView,使用NSFetchedResultsController返回Location实体。默认排序(以及节标题)是通过实体名称的第一个字母进行的。这个方法可以工作(尽管我仍在努力将大写和小写组合到同一部分中)。用户可以选择按照三个可选类别之一对表格进行排序(这些类别是实体的属性),然后这些类别按实体名称排序。
当我设置按类别排序时,会出现以下运行时错误:
[_TtCSs23_ContiguousArrayStorage00007F80513B59D0 key]: unrecognized selector sent to instance 0x7f80513b5720

这是我的NSFetchedResultsController
var sectionNameKeyPathString1: String?
var sectionNameKeyPathString2: String?

var fetchedResultsController: NSFetchedResultsController {
    if _fetchedResultsController != nil {
        return _fetchedResultsController!
    }

    let fetchRequest = NSFetchRequest()
    // Edit the entity name as appropriate.
    let entity = NSEntityDescription.entityForName("Location", inManagedObjectContext: self.managedObjectContext!)
    fetchRequest.entity = entity

    // Set the batch size to a suitable number.
    fetchRequest.fetchBatchSize = 20

    // Edit the sort key as appropriate.
    if sectionNameKeyPathString1 != nil {
        let sortDescriptor1 = NSSortDescriptor(key: sectionNameKeyPathString1!, ascending: true)
        let sortDescriptor2 = NSSortDescriptor(key: sectionNameKeyPathString2!, ascending: true)
        let sortDescriptors = [sortDescriptor1, sortDescriptor2]
        fetchRequest.sortDescriptors = [sortDescriptors]
    } else {
        let sortDescriptor = NSSortDescriptor(key: "locationName", ascending: true)
        fetchRequest.sortDescriptors = [sortDescriptor]
    }

    var sectionNameKeyPath: String
    if sectionNameKeyPathString1 == nil {
        sectionNameKeyPath = "firstLetterAsCap"
    } else {
        sectionNameKeyPath = sectionNameKeyPathString1!
    }

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: sectionNameKeyPath, cacheName: "Locations")
    aFetchedResultsController.delegate = self
    _fetchedResultsController = aFetchedResultsController

    var error: NSError? = nil
    if !_fetchedResultsController!.performFetch(&error) {
        // TODO: Handle this error
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        //println("Unresolved error \(error), \(error.userInfo)")
        abort()
    }

    return _fetchedResultsController!
}

使用断点,我相信,例如,sectionNameKeyPathString1 =“category1”和sectionNameKeyPathString2 =“locationName”,并且sectionNameKeyPath =“category1”,因此键路径与第一个排序描述符匹配。

我以前在Obj-C中实现过这个,但现在我正在烦恼并且肯定自己遭受了错误盲目症。

1个回答

11

是不是你只是有太多的 []?

    let sortDescriptors = [sortDescriptor1, sortDescriptor2] // <- an [NSSortDescriptor]
    fetchRequest.sortDescriptors = [sortDescriptors] // <- now an [[NSSortDescriptor]]

应该只是:

    fetchRequest.sortDescriptors = [sortDescriptor1, sortDescriptor2]

你在我还在编辑的时候就回答了我的问题。谢谢!等时间限制到了我会接受你的答案。人们盯着我看,我很开心。 - Magnas
太奇怪了,我也遇到了同样的问题!一定是不小心双击了方括号,由于在数组初始化器中初始化描述符并且习惯将像ObjC中那样的方法放在方括号中,所以没有注意到它。 - Ash

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接