Swift-对从Core Data检索的数组进行排序 [英] Swift - Sorting an array retrieved from Core Data

查看:100
本文介绍了Swift-对从Core Data检索的数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用从核心数据中检索的用户名对数组进行排序.我这样检索它:

I want to sort the array with usernames that I retrieve from my core data. I retrieve it like this:

override func viewDidAppear(animated: Bool) {

    let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let context:NSManagedObjectContext = appDel.managedObjectContext!
    let freq = NSFetchRequest(entityName: "User")
    let en = NSEntityDescription.entityForName("User", inManagedObjectContext: context)

    myList = context.executeFetchRequest(freq, error: nil)!

    tv.reloadData()

}

然后我像这样在tableview中设置单元格:

And later I set the cells in my tableview like this:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let CellID:NSString = "cell"
    var cell: UITableViewCell = self.tv.dequeueReusableCellWithIdentifier(CellID) as UITableViewCell
    if let ip = indexPath as Optional {

        var data:NSManagedObject = myList[ip.row] as NSManagedObject

        cell.textLabel!.text = data.valueForKeyPath("username") as String!

    }

我试图像这样在viewDidAppear函数中使用排序功能:

I tried to use the sorting function for in the viewDidAppear function like this:

 var sortedList = myList.sorted { $0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending }

但这给我一个错误,说:找不到成员'localizedCaseInsensitiveCompare'"

But this gives me an error saying: "Could not find member 'localizedCaseInsensitiveCompare'"

任何有关进行方法的建议将不胜感激.

Any suggestions on how to proceed would be appreciated.

推荐答案

我喜欢使用单独的函数来获取数组并对其进行排序.您可以使用 NSSortDescriptor 对数组进行排序.您可以将大写检查添加为排序描述符的选择器.我从本教程中获得了单独的函数思想..我用了它,它对我有用.我通过引用此Objective-C代码.

I like to use a separate function to fetch and sort the array. You sort the array by using an NSSortDescriptor. You can add the capitalization check as a selector for the sort descriptor. I got the separate function idea from this tutorial. I used it and it works for me. I got the idea for the selector idea by referencing this Objective-C code.

var objectives: [NSManagedObject]!
func fetchUsernames() {

    static let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let context: NSManagedObjectContext = appDel.managedObjectContext!

    let freq = NSFetchRequest(entityName: "User")
    let sortDescriptor = NSSortDescriptor(key: "username", ascending: true, selector: "localizedCaseInsensitiveCompare:")
    freq.sortDescriptors = [sortDescriptor]

    do {
        let fetchedResults: [NSManagedObject] = try managedContext.executeFetchRequest(freq) as! [NSManagedObject]
        if let results: [NSManagedObject] = fetchedResults {
            objectives = results
        }
    } catch let error as NSError {
        print("Error: \(error.localizedDescription)")
    }
    tableView.reloadData()
}

这篇关于Swift-对从Core Data检索的数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆