Question

I'm working on an application that has and end point my iOS app can access. I'm getting pretty far considering I'm cobbling the app together.

Anyways I'm currently stuck and would love some help. The data that comes back from the endpoint looks kinda like this:

    {
"error": false,
"posts": [
    {
        "id": 13,
        "name": "Hello World!",
        "description": "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Reprehenderit, ea aliquid saepe illum deleniti. Optio, quos, eum, mollitia aperiam assumenda quidem sit quod enim consectetur doloribus animi perferendis accusamus porro.",
        "user_id": 1,
        "category_id": 7,
        "created_at": "2014-04-15 18:49:54",
        "updated_at": "2014-04-15 18:49:54",
        "category": {
            "id": 7,
            "name": "awesome"
        },
        "user": {
            "id": 1,
            "username": "johncrossley",
            "firstname": "John",
            "lastname": "Crossley",
            "email": "john@example.com"
        }
    },
    {
        "id": 14,
        "name": "Hi There!",
        "description": "Lorem ipsum dolor sit amet, consectetur adipisicing elit.",
        "user_id": 1,
        "category_id": 6,
        "created_at": "2014-04-15 18:49:54",
        "updated_at": "2014-04-15 18:49:54",
        "category": {
            "id": 6,
            "name": "unicorns"
        },
        "user": {
            "id": 1,
            "username": "johncrossley",
            "firstname": "John",
            "lastname": "Crossley",
            "email": "john@example.com"
        }
    }
]

}

Anyways I'd like to format this in such away where the categories are grouped inside a dictionary so for example under the "unicorn" category I would see all the posts belonging to "unicorn" and the same for awesome.

Then I would be able to use this inside a UITableViewController to separate the posts into sections.

I could easily do this on the server end but rather not.. As I'd prefer the data to stay in the order it is.

Hope this makes sense and any help would be greatly appreciated!!

--

I have implemented AFNetworking so have the data as an NSArray just trying to play about with it. Still any help would be amazing.

Was it helpful?

Solution

First get the posts in a separate Array:

NSArray *posts = [myData valueForKey:"posts"];

Or, if myData is an Array:

NSArray posts = [myData objectAtIndex:indexOfPosts];
//IndexOfPosts is an Integer containing, unsurprisingly, the index os the Posts-Subarray

Declare the Dictionary and Arrays to hold the results:

NSMutableDictionary *results = [[NSMutableDictionary alloc] init];
NSMutableArray *unicorns = [NSMutableArray array];
NSMutableArray *awesome = [NSMutableArray array];

Then loop and sort:

for(NSDictionary *post in posts){
         if([[post valueForKey:"category"] valueForKey:"name"] isEqualToString:"unicorns"]){
             [unicorns addObject:post];
         }else if([[post valueForKey:"category"] valueForKey:"name"] isEqualToString:"awesome"]){
              [awesome addObject:post];
          }
    }

Then add The Arrays to the Dictionary:

 [result setObject:awesome forKey:"awesome"];
 [result setObject:unicorns forKey:"unicorns"];

That should do it.

EDIT: Same procedure with dynamic categories:

NSMutableDictionary *result;

for(NSDictionary *post in posts){
    NSString name = [[post objectForKey:"category"] objectForKey:"name"];
    if([result objectForKey:name]){
        [[result objectForKey:name] addObject:post];
    }else{
        NSMutableArray *array = [NSMutableArray arrayWithObject:post];
        [result setObject:array forKey:name];
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top