iOS:自然排序

编程入门 行业动态 更新时间:2024-10-22 18:29:47
本文介绍了iOS:自然排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个 iOS 应用程序,它使用 Core Data 来保存和检索数据.我如何以自然排序顺序获取按 NSString 类型的字段排序的数据?

I have an app for iOS that uses Core Data to save and retrieve data. How would I fetch data sorted by a field of NSString type in natural sort order?

现在的结果是:

100_title 10_title 1_title

我需要:

1_title 10_title 100_title

推荐答案

您可以使用 localizedStandardCompare: 作为核心数据获取请求的排序描述符中的选择器,例如

You can use localizedStandardCompare: as selector in the sort descriptor for the Core Data fetch request, for example

NSSortDescriptor *titleSort = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES selector:@selector(localizedStandardCompare:)]; [fetchRequest setSortDescriptors:[titleSort]];

Swift 3:

let titleSort = NSSortDescriptor(key: "title", ascending: true, selector: #selector(NSString.localizedStandardCompare)) fetchRequest.sortDescriptors = [sortDescriptor]

或更好

let titleSort = NSSortDescriptor(key: #keyPath(Entity.title), ascending: true, selector: #selector(NSString.localizedStandardCompare)) fetchRequest.sortDescriptors = [sortDescriptor]

其中Entity"是 Core Data 托管对象子类的名称.

where "Entity" is the name of the Core Data managed object subclass.

更多推荐

iOS:自然排序

本文发布于:2023-10-14 22:00:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492337.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自然   iOS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!