开发者

NSDictionary Filtering and Sorting

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;
}
)

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?


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]];

And/or NSPredicate to get some ID only:

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


There are several ways. You can use NSPredicate and use the NSArray method -filteredArrayUsingPredicate:. Or on iOS 4 you use a filtering block: -indexesOfObjectsPassingTest: then use -objectsAtIndexes: to get the objects.

Otherwise the best is you just iterate through the array and filter the items you want into a new one:

// Given a NSArray *items
NSMutableArray *filteredItems = [NSMutableArray array];
for (NSDictionary *item in items) {
  if (/* your condition */) {
    [filteredItems addObject: item];
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜