Arrays 在Swift中使用NSPredicate对CoreData结果进行排序
我目前正在用Swift编写一个简单的电话簿应用程序,需要对CoreData查询的结果进行排序 基本上,我设置了一个名为“Directory”的NSManagedObject,其中包含适当的字段名-例如,“name_f”是用户名 查询的CoreData数据库中的名称按字母顺序排列。然而,尽管第一次搜索按字母顺序返回结果,但在程序的其他区域查询数据库之后,有时不会进行进一步的搜索。我不确定这是为什么,但我想在代码中加入按任何字段对返回的数组进行主动排序的内容 我返回NSManagedObject数组的代码如下:Arrays 在Swift中使用NSPredicate对CoreData结果进行排序,arrays,search,core-data,swift,nsmanagedobject,Arrays,Search,Core Data,Swift,Nsmanagedobject,我目前正在用Swift编写一个简单的电话簿应用程序,需要对CoreData查询的结果进行排序 基本上,我设置了一个名为“Directory”的NSManagedObject,其中包含适当的字段名-例如,“name_f”是用户名 查询的CoreData数据库中的名称按字母顺序排列。然而,尽管第一次搜索按字母顺序返回结果,但在程序的其他区域查询数据库之后,有时不会进行进一步的搜索。我不确定这是为什么,但我想在代码中加入按任何字段对返回的数组进行主动排序的内容 我返回NSManagedObject数组
func SearchName(nameToSearch: String) -> NSArray {
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext! //I added the !
let request = NSFetchRequest(entityName: "Directory")
request.returnsObjectsAsFaults = false //will return the instance of the object
//request.predicate = NSPredicate(format: "name_f = %@", nameToSearch)
//request.predicate = NSPredicate(format: "name_f MATCHES '.*(\(nameToSearch)).*'")
request.predicate = NSPredicate(format: "name_f MATCHES[cd] '(\(nameToSearch)).*'")
var results:NSArray = context.executeFetchRequest(request, error: nil)
return results
}
另外,我使用以下代码从返回的结果中提取特定值(其中searchResult是SearchName所在类的实例):
理想情况下,我希望能够:
1-修改上面的代码以确保“results”数组的排序正确。
2-标识可随后应用于结果数组以按任何字段重新排序的代码
提前谢谢 谢谢-在request.predicate行之前需要以下行:
let sortDescriptor = NSSortDescriptor(key: "name_f", ascending: true)
let sortDescriptors = [sortDescriptor]
request.sortDescriptors = sortDescriptors
我使用了Oliver Spencer的上述答案,因为排序方法包括待定的更改,而“max”方法没有(请参阅) 为了方便起见,这里是我的代码:
func fetch_biggestImageID() -> Int {
print("Fetching biggest ImageID")
let request: NSFetchRequest<CD_Image> = CD_Image.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(key: "id", ascending: false)]
request.fetchLimit = 1
var maxValue: Int64? = nil
do {
let result = try self.managedObjectContext.fetch(request)
maxValue = result.first?.id
} catch {
fatalError("Unresolved error while retrieving max imageID value \(error)")
}
return Int(truncatingIfNeeded: maxValue ?? 0 )
}
func fetch_biggestImageID()->Int{
打印(“获取最大图像ID”)
let请求:NSFetchRequest=CD_Image.fetchRequest()
request.sortDescriptors=[NSSortDescriptor(键:“id”,升序:false)]
request.fetchLimit=1
变量maxValue:Int64?=nil
做{
let result=try self.managedObjectContext.fetch(请求)
maxValue=结果。第一个?.id
}抓住{
fatalError(“检索最大imageID值\(错误)时出现未解决的错误”)
}
返回Int(截断如果需要:maxValue±0)
}
您是否查看了NSPredicate
的sortDescriptors
属性?《核心数据编程指南》中也有许多示例(在Objective-C中),请参见。sortDescriptors是NSFetchRequest的属性。我不认为NSPredicate有这样的属性。@insane-36:你是对的,我的意思是NSFetchRequest。或者:request.sortDescriptors=[NSSortDescriptor(key:“name\u f”,升序:true)]
如果你不喜欢打字!
func fetch_biggestImageID() -> Int {
print("Fetching biggest ImageID")
let request: NSFetchRequest<CD_Image> = CD_Image.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(key: "id", ascending: false)]
request.fetchLimit = 1
var maxValue: Int64? = nil
do {
let result = try self.managedObjectContext.fetch(request)
maxValue = result.first?.id
} catch {
fatalError("Unresolved error while retrieving max imageID value \(error)")
}
return Int(truncatingIfNeeded: maxValue ?? 0 )
}