【问题标题】:Sort descriptors with NSFetchedResultsController - Swift使用 NSFetchedResultsController 对描述符进行排序 - Swift
【发布时间】:2014-09-05 10:33:48
【问题描述】:

我有一个 UITableView 来自 Core Data 和一个 NSFetchedResultsController 返回位置实体。默认排序(和节标题)是通过实体名称的第一个字母。这行得通(尽管我仍在尝试将大写和小写正确地组合到同一部分中。)用户可以选择按三个可选类别之一(它们是实体的属性)对表格进行排序,然后对这些类别进行排序按实体名称。

当我设置为按类别排序时,出现以下运行时错误:

[_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 中进行过这项工作,但现在我正在拔头发,并确定我患有 bug 失明。

【问题讨论】:

    标签: swift nsfetchedresultscontroller nssortdescriptor unrecognized-selector sections


    【解决方案1】:

    是不是你的 [] 太多了?

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

    应该是:

        fetchRequest.sortDescriptors = [sortDescriptor1, sortDescriptor2]
    

    【讨论】:

    • 你在我还在编辑的时候回答了我的问题。谢谢!时间到了我会接受你的回答。我很高兴人们都在盯着我看。
    • 这太奇怪了,我也发生了同样的事情!一定是无意中双击了方括号,但由于我在数组初始化器中初始化了描述符并且习惯于将类似的方法放在 ObjC 的方括号中,所以没有注意到它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-28
    相关资源
    最近更新 更多