Swift中的NSSortDescriptor [英] NSSortDescriptor in Swift

查看:66
本文介绍了Swift中的NSSortDescriptor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iOS应用程序,并且我将要存储在CoreData中的数据存储到UITableView中.数据实体具有名为 id 的属性,该属性是一个字符串,其中包含 A 后跟数字(即"A1","A2"等).

I am working on an iOS app and I have data stored in CoreData that I am loading into a UITableView. The data entities have an attribute called id which is a string that contains an A followed by a number (i.e. "A1" "A2" etc).

当我使用此代码进行排序时,最终将表按字典顺序进行排序(即"A1","A10","A11","A12","A2","A3"等)

When I use this code for sorting, I end up with the table being sorted lexicographically (i.e. "A1" "A10" "A11" "A12" "A2" "A3" etc)

let sortDescriptor = NSSortDescriptor(key: "id", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]

我真正想要的是像您期望的那样对它进行数字排序.我该怎么做呢?我知道可以将 NSComparator 添加为 NSSortDescriptor 的参数,但是我一生都无法弄清楚.预先感谢您的帮助!

What I really want is for it to be sorted numerically, as you might expect. How do I go about doing this? I know that a NSComparator can be added as an argument to NSSortDescriptor but I can't for the life of me get it figured out. Thanks in advance for any help!

推荐答案

(基于SQLite的)Core Data提取请求中的排序描述符不能使用自定义比较器,仅使用有限的一组内置"比较方法.这记录在:

Sort descriptors in a (SQLite-based) Core Data fetch request cannot use custom comparators and only a limited set of "built-in" comparison methods. This is documented in Fetch Predicates and Sort Descriptors in the "Core Data Programming Guide":

...另一方面,SQL存储编译谓词和排序SQL的描述符,并在数据库本身中评估结果.这样做主要是为了提高性能,但这意味着评估发生在非可可环境中,因此对描述符进行排序(或谓词)依靠可可就行不通.支持的排序选择器是 compare: caseInsensitiveCompare: localizedCompare: localizedCaseInsensitiveCompare: localizedStandardCompare:(后者是类似Finder的排序方式,大多数人应该使用时间).另外,您不能使用以下方式对瞬态属性进行排序SQLite存储.

... The SQL store, on the other hand, compiles the predicate and sort descriptors to SQL and evaluates the result in the database itself. This is done primarily for performance, but it means that evaluation happens in a non-Cocoa environment, and so sort descriptors (or predicates) that rely on Cocoa cannot work. The supported sort selectors are compare: and caseInsensitiveCompare:, localizedCompare:, localizedCaseInsensitiveCompare:, and localizedStandardCompare: (the latter is Finder-like sorting, and what most people should use most of the time). In addition you cannot sort on transient properties using the SQLite store.

幸运的是,有一种可以满足您的需求:

Fortunately, there is one that should fit your needs:

let sortDescriptor = NSSortDescriptor(key: "id", ascending: true,
                       selector: "localizedStandardCompare:")

localizedStandardCompare:进行类似Finder"的比较,特别是根据字符串中的数字数值.

localizedStandardCompare: does a "Finder-like" comparison and in particular treats digits within strings according to their numerical value.

对于 Swift 2.2/Xcode 7.3 及更高版本:

let sortDescriptor = NSSortDescriptor(key: "id", ascending: true
                         selector: #selector(NSString.localizedStandardCompare))

这篇关于Swift中的NSSortDescriptor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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