Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Narendar Singh

macrumors member
Original poster
Jun 22, 2012
76
0
INDIA
Hi
I have following mock data

name: Marc
city: San Rafael

name: Jubin
city: San Francisco

name: Jason
city: New York

I have to store this data in 2D array and also travers, How to do that?
 
Thanks for you quick reply, already I have gone through your approach but I had some issues.

My small code snippet is here:

Code:
NSMutableArray *listOfItem = [NSMutableArray array];
NSMutableDictionary *mDict = [[NSMutableDictionary alloc] init];
    

// here spec is NSArray which contain actual structured data ie array of some name/values strings

    for (int i=0;i<[spec count]; i++) 
    {
        // creating details and getting array from spec one by one.
        NSArray *details = [[NSArray alloc] initWithArray:[[spec objectAtIndex:i] valueForKey:@"details"]];
        
       // traversing details and adding to dictionary then adding to array
        for (int j=0; j<[details count]; j++) 
        {
            // adding name/value to the NSDictionary object
            [mDict setObject:[[details objectAtIndex:j] valueForKey:@"value"] forKey:[[details objectAtIndex:j] valueForKey:@"name"]];

            // adding dictionary to the NSArray object

            [listOfItem addObject:mDict];
        }
        // release details
        [details release];
   }

Now problem is that, how to traverse each element from array of dictionary

Code:
for (int k=0; k<[listOfItem count]; k++) 
   {
        // WHAT TO DO HERE TO GET EACH KEY/VALUE
   }

Please take a look and let me know what's wrong here OR you may suggest something else too. Thanks.
 
Last edited by a moderator:
Dictionaries & Arrays

OK, first of all I'd like to point out that there are some errors in that first code snippet. For example, you are adding mDict to listOfItem multiple times. I'd change the code to something like this:
Code:
NSMutableArray *listOfItem = [NSMutableArray array];
NSMutableDictionary *mDict;

    for (int i=0;i<[spec count]; i++) 
    {
        NSArray *details = [[spec objectAtIndex:i] valueForKey:@"details"];
        mDict = [[NSMutableDictionary alloc] init] autorelease];

        for (int j=0; j<[details count]; j++) 
        {
            [mDict setObject:[[details objectAtIndex:j] valueForKey:@"value"] forKey:[[details objectAtIndex:j] valueForKey:@"name"]];
        }
        [listOfItem addObject:mDict];
   }
 
I would suggest an array of dictionaries or perhaps even custom objects.

A custom object would have significantly less memory use and would be significantly faster than using a dictionary.

Use a dictionary only if you don't know the structure of the data. If you have pretty much the same keys in every dictionary, it's much faster to use a custom object.
 

Dictionaries are hash-tables - they can find contained objects in constant-time, but to do that they first need to take a hash of the object, and require a larger backing store (hash is mapped to index in memory).

Objects will be much faster - the offsets of member variables within the class instance are known. There is significantly less overhead in storing, as well as querying members.

No specific source is needed: if you're curious, pick up a data-structures textbook. Dictionaries are very flexible and fast, but it depends on the situation. In this simple case, a dictionary is overkill IMO.
 
Dictionaries are hash-tables - they can find contained objects in constant-time, but to do that they first need to take a hash of the object, and require a larger backing store (hash is mapped to index in memory).

Objects will be much faster - the offsets of member variables within the class instance are known. There is significantly less overhead in storing, as well as querying members.

No specific source is needed: if you're curious, pick up a data-structures textbook. Dictionaries are very flexible and fast, but it depends on the situation. In this simple case, a dictionary is overkill IMO.

OP didn't specify whether he has a dozen names or a million. So, yeah, it depends on the situation.
 
If spec already contains the actual data, why do you feel you need to populate details?

Well my actual data is in different format while I download it from the Server, I need to change its data structure.

My problem is solved for now. Thanks for all your discussion.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.