微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用NSFetchedResultsController对描述符进行排序 – Swift

我有一个UITableView从Core Data提供,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工作,但现在我把我的头发拉出来,确定我患有虫病.

是你有太多[]?
let sortDescriptors = [sortDescriptor1,sortDescriptor2] // <- an [NSSortDescriptor]
    fetchRequest.sortDescriptors = [sortDescriptors] // <- Now an [[NSSortDescriptor]]

应该只是:

fetchRequest.sortDescriptors = [sortDescriptor1,sortDescriptor2]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐