排序的NSDictionary的NSArray的斯威夫特对象 [英] Sort NSArray of NSDictionary objects in Swift

查看:187
本文介绍了排序的NSDictionary的NSArray的斯威夫特对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经取回来自API的一些JSON数据,现在有一个NSArray充分的NSDictionary对象。每一个的NSDictionary有一个键/值对名的,我想通过键/值对的NSArray的排序。

I have retrieved some JSON data from an API and now have an NSArray full of NSDictionary objects. Each NSDictionary has a key/value pair of "name" and I want to sort the NSArray by that key/value pair.

我已经做了搜索,但没有我遇到的解决方案似乎在新雨燕语言的工作相当多,我不知道这是否是可能的错误或不...

I've done quite a bit of searching but none of the solutions I've come across seem to work in the new Swift language and I'm not sure if it's possibly a bug or not...

我试过:

var descriptor: NSSortDescriptor = NSSortDescriptor(key: "name", ascending: true)
var sortedResults: NSArray = results.sortedArrayUsingDescriptors(NSSortDescriptor(key: "name", ascending: true))

但是,这不会编译指出NSSortDescriptor是无法转换为[AnyObject]。

But that won't compile stating "NSSortDescriptor is not convertible to [AnyObject]".

任何意见?

推荐答案

您必须通过某种描述符的数组(即使它只有一个):

you must pass an array of sort descriptors (even if it's only one):

var descriptor: NSSortDescriptor = NSSortDescriptor(key: "name", ascending: true)
var sortedResults: NSArray = results.sortedArrayUsingDescriptors([descriptor])

这篇关于排序的NSDictionary的NSArray的斯威夫特对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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