gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 22:30:12 24 4
gpt4 key购买 nike

我有一个 UITableViewCore 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 中工作过,但现在我正在拔头发,并且确定我正在遭受 bug-blindness 的困扰。

最佳答案

是不是你的[]太多了?

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

应该只是:

    fetchRequest.sortDescriptors = [sortDescriptor1, sortDescriptor2]

关于swift - 使用 NSFetchedResultsController 对描述符进行排序 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25683889/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com