iOS排序NSDictionary [英] IOS Sort NSDictionary

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

问题描述

首先,这是我的代码:

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL
                                                          URLWithString:url]];

    NSData *response = [NSURLConnection sendSynchronousRequest:request
                                             returningResponse:nil error:nil];

    NSDictionary *publicTimeline = [NSJSONSerialization JSONObjectWithData:response       options:0 error:&jsonParsingError];

    for (NSObject* key in publicTimeline) {
        id value = [publicTimeline objectForKey:key];
        NSLog(@"%@", key);
    }

我在Web服务上几乎没有收到任何消息,并在tableView中显示它.

I take few news on a webservice, and i show it in a tableView.

我的问题是这些新闻没有按顺序显示.

My problem is that these news aren't show in order.

在我的Web服务中,新闻是按顺序排列的,例如我有:

In my webservice, the news are in order, for example i have :

    {"0":{"title":"News1"}}
    {"1":{"title":"News2"}}
    {"2":{"title":"News3"}}

等.

但是在循环中,我松开"了这个顺序:/而且我想按此顺序显示我的新闻,首先是索引为"0"的新闻,之后是"1"等...

but in the loop, i "loose" this order :/ And i want to have my news in this order, first the news with the index "0" after "1" etc...

(NSDictionary似乎取消了此顺序,例如,在我的代码之后,我在表视图中看到了新闻:"2","0","1",而不是我的新闻"0","1 ," 2.)

(the NSDictionary seems to loose this order, for example, after my code, i have in my tableview, the news : "2", "0", "1", and not my news "0", "1", "2". )

(我尝试了一些答案,但在我的情况下似乎没有任何效果:()

(i tried some answers, but none seems to work in my case :( )

有人帮我吗? thx,

SOmeone to help me ? thx,

推荐答案

这是我上次用来对字典中的键进行排序的内容.希望您可以更轻松地实现它:)

This is what I used last time to sort the keys of my dictionary. Hope it will be easier for you to implement it :)

NSMutableArray *sortedArray = [NSMutableArray arrayWithArray:publicTimeline.allKeys];
[sortedArray sortUsingSelector:@selector(localizedStandardCompare:)];

您应该将已排序的键放在sortedArray

And you should have your sorted keys in sortedArray

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

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