NSDictionary筛选和排序

NSDictionary筛选和排序

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

问题描述

我正在开发一个使用JSON API的应用程序,并且正在使用Objective C Framework的JSON将数据放入NSDictionary中.字典看起来像这样:

I am working on a App that makes use of a JSON API and I am Using the JSON for Objective C Framework to get the Data into a NSDictionary. The dictionary loos like this:

({
    admitted = "<null>";
    agreed = "<null>";
    "area_id" = 1;
    "area_name" = "Digitales, Urheber-/Patentrecht, Datenschutz";
    created = "2010-08-17 12:41:03";
    "current_draft_content" = Bla Bla Bla";
    "current_draft_created" = "2010-08-17 12:42:44";
    "current_draft_formatting_engine" = compat;
    "discussion_url" = "http://piratenpad.de/OpenSourceZwang";
    id = 502;
    "informed_supporter_count" = 11;
    "issue_accepted" = "";
    "issue_admission_time" = "8 days";
    "issue_closed" = "";
    "issue_created" = "2010-08-17 12:41:03";
    "issue_discussion_time" = "15 days";
    "issue_fully_frozen" = "";
    "issue_half_frozen" = "";
    "issue_id" = 242;
    "issue_population" = 372;
    "issue_ranks_available" = 0;
    "issue_state" = new;
    "issue_verification_time" = "8 days";
    "issue_vote_later" = 0;
    "issue_vote_now" = 0;
    "issue_voter_count" = "<null>";
    "issue_voting_time" = "8 days";
    name = "Alte & unsupportete Software, muss Open Source werden";
    "negative_votes" = "<null>";
    "policy_initiative_quorum_den" = 100;
    "policy_initiative_quorum_num" = 10;
    "policy_issue_quorum_den" = 100;
    "policy_issue_quorum_num" = 10;
    "policy_majority_den" = 2;
    "policy_majority_num" = 1;
    "policy_majority_strict" = 1;
    "positive_votes" = "<null>";
    rank = "<null>";
    revoked = "<null>";
    "satisfied_informed_supporter_count" = 7;
    "satisfied_supporter_count" = 7;
    "suggested_initiative_id" = "<null>";
    "supporter_count" = 11;
},
    {
    admitted = "<null>";
    agreed = "<null>";
    "area_id" = 1;
    "area_name" = "Digitales, Urheber-/Patentrecht, Datenschutz";
    created = "2010-08-17 14:09:14";
    "current_draft_content" = "Bla Bla Bla";
    "current_draft_created" = "2010-08-17 14:09:31";
    "current_draft_formatting_engine" = compat;
    "discussion_url" = "";
    id = 508;
    "informed_supporter_count" = 0;
    "issue_accepted" = "2010-08-14 01:51:45";
    "issue_admission_time" = "15 days";
    "issue_closed" = "";
    "issue_created" = "2010-08-14 01:34:26";
    "issue_discussion_time" = "30 days";
    "issue_fully_frozen" = "";
    "issue_half_frozen" = "";
    "issue_id" = 30;
    "issue_population" = 603;
    "issue_ranks_available" = 0;
    "issue_state" = accepted;
    "issue_verification_time" = "15 days";
    "issue_vote_later" = 9;
    "issue_vote_now" = 0;
    "issue_voter_count" = "<null>";
    "issue_voting_time" = "15 days";
    name = "Grundrecht: Alternative Formulierung";
    "negative_votes" = "<null>";
    "policy_initiative_quorum_den" = 100;
    "policy_initiative_quorum_num" = 10;
    "policy_issue_quorum_den" = 100;
    "policy_issue_quorum_num" = 10;
    "policy_majority_den" = 3;
    "policy_majority_num" = 2;
    "policy_majority_strict" = 0;
    "positive_votes" = "<null>";
    rank = "<null>";
    revoked =         {
    };
    "satisfied_informed_supporter_count" = 0;
    "satisfied_supporter_count" = 0;
    "suggested_initiative_id" = "<null>";
    "supporter_count" = 0;
}
)

什么是获取"issue_id"为2或6以上的所有对象的好方法,或者显示成组的UITabeView并将具有相同"issue_id"的所有对象归为一组的一种好方法?

What is a good way to get all object where the "issue_id" is 2 oder 6 or what ever to display a grouped UITabeView with all objects with the same "issue_id" in one group?

推荐答案

您可以使用NSSortDescriptor进行排序:

You can use NSSortDescriptor to sort:

    NSSortDescriptor *dateSortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"issue_id" ascending:YES selector:@selector(compare:)] autorelease];
    [youDictionaryArray sortUsingDescriptors:[NSArray arrayWithObjects:dateSortDescriptor, nil]];

和/或NSPredicate仅获得一些ID:

And/or NSPredicate to get some ID only:

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(issue_id == %i)", 2];
    NSArray *newArray = [youDictionaryArray filteredArrayUsingPredicate:predicate];

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

07-22 12:57