0

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;
    }
1
  • Consider using NSDictionary better than this 2 dimensional array. Let me know if you need info about NSDictionary Commented Jan 8, 2014 at 6:36

5 Answers 5

7

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"];
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.