jenjenjen
jenjenjen

Reputation: 29

How to create a Multidimensional Array using loop in IOS xcode

I would like to create an array for iOS platform like the PHP syntax below, many thanks ~~

$createArray = array ();

    for ($i = 0; $i<10; $i++) {

    $createArray[$i]['name'] = $name;
    $createArray[$i]['age'] = $age;
    }

Upvotes: 0

Views: 8300

Answers (5)

Prasad tj
Prasad tj

Reputation: 19

First you to have set An NSMutableDictionary on .h file

        @interface MSRCommonLogic : NSObject
        {
            NSMutableDictionary *twoDimensionArray;
        }

        then have to use following functions in .m file


        - (void)setValuesToArray :(int)rows cols:(int) col value:(id)value
        {
            if(!twoDimensionArray)
            {
                twoDimensionArray =[[NSMutableDictionary alloc]init];
            }

            NSString *strKey=[NSString stringWithFormat:@"%dVs%d",rows,col];
            [twoDimensionArray setObject:value forKey:strKey];

        }

        - (id)getValueFromArray :(int)rows cols:(int) col
        {
            NSString *strKey=[NSString stringWithFormat:@"%dVs%d",rows,col];
            return  [twoDimensionArray valueForKey:strKey];
        }

Upvotes: 0

Tirth
Tirth

Reputation: 7789

Save your values in NSDictionary and add that dictionary into your array

NSMutableArray *theArray =  [NSMutableArray array];

    for (int indexValue = 0; indexValue<10; indexValue++) {
       NSMutableDictionary *theDictionary = [[NSMutableDictionary alloc] init];
       [theDictionary setObject:name forKey:@"name"];
       [theDictionary setObject:age forKey:@"age"];
       [theArray addObject:theDictionary]
  }

While Retrieving time,

NSString *name = [[theArray objectAtIndex:indexValue] objectForKey:@"name"];
NSString *age = [[theArray objectAtIndex:indexValue] objectForKey:@"age"];

Upvotes: 7

nick
nick

Reputation: 114

you might find it helpful:

array = [[NSMutableArray alloc] init];

for (int i = 0; i < 8; i++) {
    NSMutableArray *subArray = [[NSMutableArray alloc] init];
    for (int j = 0; j < 8; j++) {
        [subArray addObject:[NSNumber numberWithInt:0]]; 
    }
    [array addObject:subArray];
    [subArray release];
}

also check this question

Upvotes: 2

Mani
Mani

Reputation: 17595

You can use this. But this is not better way in IOS.

    NSMutableArray *array[20];
    for (int i=0;i< 20; i++)
    {
        array[i] = [NSMutableArray array];
        for (int j=0;j<3;j++)
        {
             NSMutableDictionary *theDictionary = [[NSMutableDictionary alloc] init];
            [theDictionary setObject:name forKey:@"name"];
             [theDictionary setObject:age forKey:@"age"];
             [[array[i] addObject:theDictionary]
        }
    }

Upvotes: 0

Kumar KL
Kumar KL

Reputation: 15335

Try this.

array = [[NSMutableArray alloc] init];

    for (int i = 0; i < 10; i++) {
        NSMutableArray *subArray = [[NSMutableArray alloc] init];
        for (int j = 0; j < 2; j++) {
    //Do your Stuff
           // [subArray addObject:name]; 
             // [subArray addObject:Age]; 
        }
        [array addObject:subArray];

    }

OR

Why can't try with the NSDictionary

Upvotes: 0

Related Questions