How to create and traverse 2D NSMutuable Array in iOS

Discussion in 'iOS Programming' started by Narendar Singh, Jun 27, 2012.

  1. Narendar Singh macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #1
    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?
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    I would suggest an array of dictionaries or perhaps even custom objects.
     
  3. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #3
    Like Dejo said, there are some ways to approach this.
    Apple has provided something called Dictionary's for this.
    But i'd take the Custom NSObject over Dictionary any day.
     
  4. Narendar Singh, Jun 28, 2012
    Last edited by a moderator: Jun 28, 2012

    Narendar Singh thread starter macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #4
    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.
     
  5. Ides macrumors member

    Joined:
    Mar 27, 2012
    #5
    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];
       }
    
     
  6. Narendar Singh thread starter macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    If spec already contains the actual data, why do you feel you need to populate details?
     
  8. Saladinos macrumors 68000

    Saladinos

    Joined:
    Feb 26, 2008
    #8
    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.
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Source?
     
  10. Saladinos macrumors 68000

    Saladinos

    Joined:
    Feb 26, 2008
    #10
    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.
     
  11. samdev macrumors regular

    Joined:
    Sep 16, 2011
    #11
    OP didn't specify whether he has a dozen names or a million. So, yeah, it depends on the situation.
     
  12. Narendar Singh thread starter macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #12
    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.
     

Share This Page