如何在 IOS xcode 中使用循环创建多维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20988502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:31:36  来源:igfitidea点击:

How to create a Multidimensional Array using loop in IOS xcode

iosiphoneobjective-cxcodeios7

提问by jenjenjen

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

我想为iOS平台创建一个数组,如下面的PHP语法,非常感谢~~

$createArray = array ();

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

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

回答by Tirth

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

将您的值保存在 NSDictionary 中并将该字典添加到您的数组中

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

回答by nick

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

也检查这个问题

回答by Prasad tj

First you to have set An NSMutableDictionary on .h file

首先,您在 .h 文件上设置了 NSMutableDictionary

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

回答by Mani

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

你可以用这个。但这在 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]
        }
    }

回答by Kumar KL

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

为什么不能尝试 NSDictionary